Repository navigation
Conversation
Resolves quantumlib#8402 by falling back to cirq.has_unitary(val) and cirq.unitary(val) when an object lacks a direct _mixture_ or _unitary_ method. This matches the behavior of cirq.has_mixture and cirq.kraus for decomposable unitary objects such as Moment and CircuitOperation. Signed-off-by: Himmat Sirwee <himmatsirwee05@gmail.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8406 +/- ##
=======================================
Coverage 99.59% 99.59%
=======================================
Files 1133 1133
Lines 103919 103934 +15
=======================================
+ Hits 103498 103513 +15
Misses 421 421 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
arettig
left a comment
There was a problem hiding this comment.
Hello, thanks for looking into this. I have a couple minor comments below. The main thing is that now that mixture can build the unitary from the decomposition, we should not call that potentially slow function from has_mixture.
There was a problem hiding this comment.
has_mixture should also be changed. Here, mixture will now form the full unitary via decomposition if possible, which can be very large/slow and disregards the allow_decompose parameter. Since the decomposition is checked above, this should change to just call _mixture_ here if it exists.
|
|
||
| def test_mixture_decomposable_unitary() -> None: | ||
| q0, q1 = cirq.LineQubit.range(2) | ||
| for val in [ |
There was a problem hiding this comment.
Please use pytest.mark.parameterize rather than a loop here.
There was a problem hiding this comment.
Please update this docstring as well.
- In has_mixture: check _mixture_ directly on fallback instead of calling mixture(val), avoiding expensive unitary construction through decomposition and respecting allow_decompose. - Update mixture docstring to describe unitary fallback behavior. - Use pytest.mark.parametrize in test_mixture_decomposable_unitary instead of a loop and add test verifying allow_decompose=False. Signed-off-by: Himmat Sirwee <himmatsirwee05@gmail.com>
072c785 to
b440201
Compare
|
Hi @arettig , thanks for the review! I've addressed all the requested changes in the latest commit: has_mixture: Replaced the fallback call to mixture(val, None) with directly checking mixture. This prevents constructing full unitaries via decomposition and properly honors the allow_decompose parameter. Added test_has_mixture_allow_decompose_false to verify this behavior. |
Signed-off-by: Himmat Sirwee <himmatsirwee05@gmail.com>
Fixes #8402
Description
cirq.has_mixture(val)returnsTruefor decomposable unitary objects (such asMomentandCircuitOperation), butcirq.mixture(val)was raisingTypeErrorbecause it only inspectedval._unitary_and never fell back tocirq.has_unitary(val)/cirq.unitary(val).This PR aligns
cirq.mixturewithcirq.krausby falling back tocirq.unitary(val)whenevercirq.has_unitary(val)isTrue.Testing
test_mixture_decomposable_unitarytestingcirq.Momentandcirq.CircuitOperation.cirq-core/cirq/protocols/(1228 passed).