Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for CCZ and CCX/TOF gates. #487

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MichaelBroughton
Copy link
Collaborator

@MichaelBroughton MichaelBroughton commented Feb 22, 2021

This PR adds serialization and C++ implementation support for three qubit eigen gates. A lot of this code is copy pasted from the two qubit cases with tweaks to work for the three qubit cases. Currently tests will fail on our unitary op since the latest qsim (https://github.com/quantumlib/qsim/blob/278b217f94bca179ac5fafd85498ee28bbd3e236/lib/unitary_calculator_basic.h#L52) unitary calculator doesn't support three qubit gates. This is being worked on by @sergeisakov so we should be able to merge very soon once we can upgrade our qsim dependency. Fixes #480

@MichaelBroughton MichaelBroughton marked this pull request as draft February 22, 2021 19:02
@MichaelBroughton
Copy link
Collaborator Author

@jaeyoo Would you be able to take a look at this issue ? It seems like the ParameterShift differentiator is breaking with adding the new gates. I did some investigation and I think it may have actually been broken by one of the recent PRs we did (since it still breaks even without the new gates sometimes if I increase the depth of the circuits). Do you think you would be able to take a look and see if you can spot the fix ?

@jaeyoo jaeyoo self-requested a review March 5, 2021 23:45
@jaeyoo
Copy link
Member

jaeyoo commented Mar 5, 2021

@jaeyoo Would you be able to take a look at this issue ? It seems like the ParameterShift differentiator is breaking with adding the new gates. I did some investigation and I think it may have actually been broken by one of the recent PRs we did (since it still breaks even without the new gates sometimes if I increase the depth of the circuits). Do you think you would be able to take a look and see if you can spot the fix ?

Let me look at it during weekend, and let's talk about it at our next sync.

Copy link
Member

@jaeyoo jaeyoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good news! I found out the fix.

@@ -75,6 +75,20 @@ void CreateGradientCircuit(
grad_gates->push_back(grad);
}

// Three qubit Eigen.
else if (circuit.gates[i].kind == qsim::Cirq::GateKind::kCCZPowGate ||
circuit.gates[i].kind == qsim::Cirq::GateKind::kCCXPowGate) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good news! I fixed it Mike. The culprit was qsim::Cirq::GateKind::kCCX for Toffoli gate. It has its own enum value. :)

Please add the following line:

else if (circuit.gates[i].kind == qsim::Cirq::GateKind::kCCZPowGate ||
             circuit.gates[i].kind == qsim::Cirq::GateKind::kCCXPowGate ||
             circuit.gates[i].kind == qsim::Cirq::GateKind::kCCX) {

and then I saw the following test passed.

bazel test tensorflow_quantum/python/differentiators:gradient_test

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't think that is the fix :(. Like we talked about on our sync it is the parametershift differentiator that is having issues, not the adjoint differentiator. The tests fail somewhat randomly and unpredictably so If you can get the parametershift test to pass with --runs_per_test=50 and with many more circuits then I think that would be enough to say that the bug is fixed. When I run with this change I can still get the error if I change the number of circuits and ops.

@jaeyoo
Copy link
Member

jaeyoo commented Mar 10, 2021

Oops, right, thank you for the catch. I am working on it, and found out the broken case in ParameterShift.

@jaeyoo
Copy link
Member

jaeyoo commented Mar 15, 2021

Hi Mike, I want to ask you some questions.

I am trying to figure out the culprit circuit, but it seems not yet supported for multiple controlled qubit ops.
For example, at the test test_gradients_vs_cirq_finite_difference in gradients_test.py, I used

        # will this be too tight? time will tell.
        self.assertAllClose(cirq_grads, tfq_grads, rtol=2e-2, atol=2e-2,
                            msg=f"{util.from_tensor(circuit_batch)}")

and got this error:

Traceback (most recent call last):
  File "/usr/local/google/home/jaeyoo/tfq-venv/lib/python3.8/site-packages/absl/testing/parameterized.py", line 282, in bound_param_test
    return test_method(self, **testcase_params)
  File "/usr/local/google/home/jaeyoo/.cache/bazel/_bazel_jaeyoo/7da1e95e8bb125cc5976e7c817e098d4/execroot/__main__/bazel-out/k8-opt/bin/tensorflow_quantum/python/differentiators/gradie
nt_test.runfiles/__main__/tensorflow_quantum/python/differentiators/gradient_test.py", line 225, in test_gradients_vs_cirq_finite_difference
    msg=f"{util.from_tensor(circuit_batch)}")
  File "/usr/local/google/home/jaeyoo/.cache/bazel/_bazel_jaeyoo/7da1e95e8bb125cc5976e7c817e098d4/execroot/__main__/bazel-out/k8-opt/bin/tensorflow_quantum/python/differentiators/gradie
nt_test.runfiles/__main__/tensorflow_quantum/python/util.py", line 380, in from_tensor
    found_item = _parse_single(item)
  File "/usr/local/google/home/jaeyoo/.cache/bazel/_bazel_jaeyoo/7da1e95e8bb125cc5976e7c817e098d4/execroot/__main__/bazel-out/k8-opt/bin/tensorflow_quantum/python/differentiators/gradie
nt_test.runfiles/__main__/tensorflow_quantum/python/util.py", line 330, in _parse_single
    raise TypeError('Error decoding item: ' + str(item))
TypeError: Error decoding item: C1C1C0CZ**0.648900484520275*a((0, 2), (0, 4), (0, 1), (0, 3), (0, 0))

Any help please?

@MichaelBroughton
Copy link
Collaborator Author

Hmmm is circuit_batch something has had tfq.convert_to_tensor called on it ?

@yudeep-rajbhandari
Copy link

Is there any update on this?

jaeyoo pushed a commit to jaeyoo/quantum that referenced this pull request Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multi-qubit X gate promoted to TOFFOLI gate and could not be serialized by TFQ.
3 participants