-
Notifications
You must be signed in to change notification settings - Fork 3k
add_control: check for predefined gates by type instead of name #3853
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
Changes from 5 commits
609dda5
b814cc7
a1cabb8
30c9fae
4556eda
ba1dc81
8865cd8
8bcd1a1
3e99934
a11bff7
2a46dc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| fixes: | ||
| - | | ||
| Fixes cases in add_control(). Non-standard gates that were unfortunately named | ||
| identically to some standard ones (in particular, X, RX, RY, RZ) would be | ||
| identified as those gates, and the control would be applied erroneously. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,13 +169,19 @@ def test_single_controlled_composite_gate(self): | |
| ref_mat = execute(qc, simulator).result().get_unitary(0) | ||
| self.assertTrue(matrix_equal(cop_mat, ref_mat, ignore_phase=True)) | ||
|
|
||
| def test_multi_control_u3(self): | ||
| @data( | ||
| [0.2, -pi/2, pi/2], # handle as rx | ||
| [0.2, 0, 0], # handle as ry | ||
| [0, 0, 0.4], # handle as rz | ||
| [0.2, 0.3, 0.4], | ||
| ) | ||
| def test_multi_control_u3(self, params): | ||
| """Test the matrix representation of the controlled and controlled-controlled U3 gate.""" | ||
| import qiskit.extensions.standard.u3 as u3 | ||
|
|
||
| num_ctrl = 3 | ||
| # U3 gate params | ||
| alpha, beta, gamma = 0.2, 0.3, 0.4 | ||
| [alpha, beta, gamma] = params | ||
|
|
||
| # cnu3 gate | ||
| u3gate = u3.U3Gate(alpha, beta, gamma) | ||
|
|
@@ -626,6 +632,13 @@ def test_inverse_circuit(self, num_ctrl_qubits): | |
| np.testing.assert_array_almost_equal(result.data, | ||
| np.identity(result.dim[0])) | ||
|
|
||
| def test_named_circuit(self): | ||
| """Tests control is applied to operation type, not name""" | ||
| qc = QuantumCircuit(1, name='x') | ||
| gate = qc.to_gate() | ||
|
|
||
| self.assertIsNone(gate.control().definition) # Not a CnotGate | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ewinston looks like a change in #3739, specifically here: is causing the definition get in this test to throw with Is that ok/expected, since the gate here is degenerate? I originally added no operations to the test circuit, since it wouldn't be relevant to the testing the name bug. If so i'll add some arbitrary gate to the circuit.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in this special case, where the controlled gate has an empty to |
||
|
|
||
| @data(1, 2, 3, 4, 5) | ||
| def test_controlled_unitary(self, num_ctrl_qubits): | ||
| """Test the matrix data of an Operator, which is based on a controlled gate.""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 was going to add a case for cx below, but realized it's already handled here for arbitrary controls. Is it worth doing the same for the other gate checks below?
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 might be good to do for the rotation gates as well. It would be interesting to check if it reduces gate count.
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.
@ewinston gave this a shot in the commit below, but it's breaking add_control for RZ and CRZ gates. Not sure where to start investigating... any ideas where to dig further?
Uh oh!
There was an error while loading. Please reload this page.
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'm not sure why this breaks but if you remove the
orcondition in_operation_has_base-gatethe issue seems to resolve. Then it's justisinstance(operation, gate)like in original logic.