-
-
Notifications
You must be signed in to change notification settings - Fork 163
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
Resolve issue 1073 handle unsupported gates #2585
Resolve issue 1073 handle unsupported gates #2585
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2585 +/- ##
=======================================
Coverage 98.73% 98.73%
=======================================
Files 92 92
Lines 4174 4176 +2
=======================================
+ Hits 4121 4123 +2
Misses 53 53 ☔ View full report in Codecov by Sentry. |
Great explanation of the situation, thanks @gluonhiggs !
|
Thank you for the information! I think |
0dbdc7e
to
5923a2b
Compare
LGTM, very clean! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice changes, and tests @gluonhiggs! What do you think about adding a warning when we fall into the except QasmException
block? This way the user knows the circuit they are working with might have been changed by our code.
test_qc = qiskit.QuantumCircuit(2) | ||
test_qc.rx(0.1, 0) | ||
test_qc.ry(0.1, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circuit does not contain an RYY
gate. Should the test be renamed or an RYY
gate added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a typo. ry
should be replaced by ryy
.
def test_convert_qiskit_to_mitiq_circuit_conversion_error(): | ||
# Custom gates are not supported in conversions | ||
gate = Operator([[0.0, 1.0], [-1.0, 0.0]]) | ||
qreg = QuantumRegister(1) | ||
circ = QuantumCircuit(qreg) | ||
circ.unitary(gate, [0]) | ||
|
||
with pytest.raises( | ||
CircuitConversionError, match="Circuit could not be converted to" | ||
): | ||
convert_to_mitiq(circ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we should remove this test? It does seem to belong in mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py
, but I think it's still useful, right? If the error is not raised anymore (as I think is indicated in #2585 (comment)), then can we change the test to assert the conversion to something happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was originally useful for ensuring the detection of unsupported custom gates during conversion. However, in theory, suppose that we can resolve #1073 by this PR, there will be no errors produced in any circuit conversion from qiskit to mitiq. In particular, with the current code (in the PR), we still can convert the circuit without raising any error. That's why I removed the test.
Probably, we could update the test to check if the conversion works correctly for circuits with custom gates, e.g., transpile
is called in here (or the except
block is executed).
except QasmException:
# Try to decompose circuit before running
# This is necessary for converting qiskit circuits with
# custom packaged gates, e.g., QFT gates
BASIS_GATES = [
"sx",
"sxdg",
"rx",
"ry",
"rz",
"id",
"u1",
"u2",
"u3",
"r",
"x",
"y",
"z",
"h",
"s",
"t",
"cx",
"cy",
"cz",
"ch",
"swap",
"cswap",
"ccx",
"sdg",
"tdg",
]
circuit = qiskit.transpile(circuit, basis_gates=BASIS_GATES)
mitiq_circuit = from_qasm(qasm2.dumps(circuit))
I'd love to hear your thoughts on such an adaptation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All that makes sense! My request is that we add one more test, to ensure a circuit with a qiskit.quantum_info.operators.Operator
object can be converted correctly. This test verified as such, and we no longer have a test that checks this, if we remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have committed a simple test for that. Please have a look!
5923a2b
to
0b36e41
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Just one question, but can be merged once it's checked.
Thank you for your contribution.
Resolve #1073
Description
I have changed the logic in
from_qiskit
as follows:qiskit
circuit tomitiq
circuit.["rxx", "rzz", "rzx", "ryy", "QFT"]
, we decompose the these gates first usingdecompose()
withreps=10
, to ensure there will not be the same gates after the decompositions (because in fact, there will be if we only decompose once).PassManager
(refer here.Reason
rxx
,ryy
,rzz
,rzx
) that have decomposition problem togates_to_decompose
argument asSo, maybe, we only need to add
sx
,u
,p
,cu1
,ecr
toGATES_TO_DECOMPOSE
. It turned out that these gates are unchanged under the decomposition.transpile
to deal with circuits containing unsupported gates, but this approach caused something like "over-transpilation", then failedtest_convert_qiskit_to_mitiq_circuit_conversion_error
andtest_folding_circuit_conversion_error_qiskit
(1). Therefore, we didn't choose this approach in Fix converting Rxx and similar Qiskit gates #2579. However, I have been thinking about mixing this approach withdecompose()
(a hybrid approach somehow). I tried the hybrid approach to handlesx
,u
,p
,cu1
,ecr
, e.g.,The idea is that we decompose
rij
first, then we transpile the circuit left withsx
,u
,p
,cu1
,ecr
to thebasis_gates
.And, many tests passed, the tests (1) failed. "over-transpilation" still happens.
Attempt 3: Is there any way that we can transpile the circuits partially, I asked, like transpile only
sx
,u
,p
,cu1
,ecr
.There is no exact same thing, but there is
PassManager
. The failed tests (1) happened because we don't want to convert custom gates to mitiq, and we don't want conversions that, as I understand, folding method can't handle the circuit, like ZNE shouldn't be able to applied to custom gates? AndPassManager
can help this, because it optimize the circuit transformation in hardware-compatible form. And maybe that's what folding verification is about.I'm still not entirely clear on this, why do we need these tests, could you please explain? Thank you
Attempt 4: I asked myself why don't we use
PassManager
from the beginning instead of usingdecompose()
, e.g.,I tried that,
convert_to_mitiq
failed forQFT
sx
, but I still added the corresponding test. I added tests foru
,p
,cu1
,ecr
. There is no issue. I created new circuits with combination of these["rxx", "rzz", "rzx", "ryy", "QFT"]
andu
,p
,cu1
,ecr
for testing. With thisError happened.
decompose
might still remainQFT
(and other similar gates) in the circuit . Therefore, I addedreps = 10
to thedecompose
.License
Before opening the PR, please ensure you have completed the following where appropriate.
I updated the documentation where relevantno doc updated.