-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Decompose cx into cz and two hadamards #3067
Conversation
|
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.
Hi @Wikstahl , thanks for the contribution! This PR raises an interesting question about our Instruction.__eq__
( https://github.com/Qiskit/qiskit-terra/blob/5aaf6e7/qiskit/circuit/instruction.py#L92 ).
Currently, we check if two Instruction
s are equal by checking if their definition
s are equal. With this PR, cx
would be defined in terms of cz
, and cz
would be defined in terms of cx
, so this check will recur infinitely.
I think there are two possible paths forward:
- Stop comparing
definition
s. ConsiderInstructions
equal if they matchname
,num_qubits
andnum_clbits
. - Make
definition
comparison smart enough to detect mutual definitions like this, and consider them equal.
I tested 1) briefly and all our tests passed, though we'd probably want to think more about if the definition
check is required anywhere.
Hi, let’s not use “definition” for this. Definition was supposed to only be 1 thing. And the best use case is when you build up a composite gate from elementary gates and want to track how that gate is defined in terms of those elementary gates.
I think we need a new field “decompositions”. This can be multiple rules of rewriting some gate. For example there are many ways of decomposing a Toffoli. The decision of which one to use would be left to the transpiler (the unroller pass or some other pass).
I’m thinking:
gate.decompositions gives the list of rules defined for decomposing that gate
gate.add_decomposition() would add a new rule (if a user knows some better decomposition say)
gate.set_decomposition() would override and force the transpiler to use that particular decomposition rule
… On Sep 9, 2019, at 7:18 PM, Kevin Krsulich ***@***.***> wrote:
@kdk commented on this pull request.
Hi @Wikstahl , thanks for the contribution! This PR raises an interesting question about our Instruction.__eq__ ( https://github.com/Qiskit/qiskit-terra/blob/5aaf6e7/qiskit/circuit/instruction.py#L92 ).
Currently, we check if two Instructions are equal by checking if their definitions are equal. With this PR, cx would be defined in terms of cz, and cz would be defined in terms of cx, so this check will recur infinitely.
I think there are two possible paths forward:
Stop comparing definitions. Consider Instructions equal if they match name, num_qubits and num_clbits.
Make definition comparison smart enough to detect mutual definitions like this, and consider them equal.
I tested 1) briefly and all our tests passed, though we'd probably want to think more about if the definition check is required anywhere.
In test/python/transpiler/test_unroll_cx.py:
> +
+
+class TestUnrollCX(QiskitTestCase):
+ """Tests the unrolling of CX into CZ."""
+
+ def test_cx_unroll(self):
+ """Test decompose a CX into CZ and Hadamards.
+ q0:-----.----- q0:---------.---------
+ | |
+ q1:----(+)---- = q1:---[H]--[z]--[H]---
+ """
+ qr = QuantumRegister(2, 'qr')
+ circuit = QuantumCircuit(qr)
+ circuit.cx(qr[0], qr[1])
+ dag = circuit_to_dag(circuit)
+ pass_ = Unroller(['cz','h'])
⬇️ Suggested change
- pass_ = Unroller(['cz','h'])
+ pass_ = Unroller(['cz', 'h'])
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub, or mute the thread.
|
pep8 change Co-Authored-By: Kevin Krsulich <[email protected]>
I'm putting this one |
This is superseded by the RFC here: Qiskit/RFCs#6 |
Summary
It is now possible to unroll a cx gate into a cz and two hadamards. This can for e.g. be used when transpiling a circuit containing 'cx' gates into 'cz' and 'h' gates.
Details and comments
To avoid circular import the CzGate is imported inside the _define function.
Added a test file that verify if the unrolling works