Skip to content

Commit

Permalink
Cleaning up U3 in the unitary extension (#10597)
Browse files Browse the repository at this point in the history
* Clean up to use U vs U3

Removing U3 from unitary and moving it to the U gate

* fixing test

* Create unitary-u-not-u3-0a32c2d968f7890e.yaml
  • Loading branch information
jaygambetta authored Aug 10, 2023
1 parent f6207c0 commit 77801c8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions qiskit/extensions/unitary.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
from qiskit.circuit import QuantumRegister, Qubit
from qiskit.circuit.exceptions import CircuitError
from qiskit.circuit._utils import _compute_control_matrix
from qiskit.circuit.library.standard_gates import U3Gate
from qiskit.circuit.library.standard_gates import UGate
from qiskit.extensions.quantum_initializer import isometry
from qiskit.quantum_info.operators.predicates import matrix_equal
from qiskit.quantum_info.operators.predicates import is_unitary_matrix
from qiskit.quantum_info.synthesis.one_qubit_decompose import OneQubitEulerDecomposer
from qiskit.quantum_info.synthesis.two_qubit_decompose import two_qubit_cnot_decompose
from qiskit.extensions.exceptions import ExtensionError

_DECOMPOSER1Q = OneQubitEulerDecomposer("U3")
_DECOMPOSER1Q = OneQubitEulerDecomposer("U")


class UnitaryGate(Gate):
Expand Down Expand Up @@ -125,7 +125,7 @@ def _define(self):
q = QuantumRegister(1, "q")
qc = QuantumCircuit(q, name=self.name)
theta, phi, lam, global_phase = _DECOMPOSER1Q.angles_and_phase(self.to_matrix())
qc._append(U3Gate(theta, phi, lam), [q[0]], [])
qc._append(UGate(theta, phi, lam), [q[0]], [])
qc.global_phase = global_phase
self.definition = qc
elif self.num_qubits == 2:
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/unitary-u-not-u3-0a32c2d968f7890e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
upgrade:
- |
the definition of UnitaryGate for single qubit unitaries is now in terms of UGate instead of the legacy U3Gate class
8 changes: 4 additions & 4 deletions test/python/circuit/test_circuit_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_unitary_qasm(self):
qasm = qc.qasm()
expected = """OPENQASM 2.0;
include "qelib1.inc";
gate unitary q0 { u3(0,0,0) q0; }
gate unitary q0 { u(0,0,0) q0; }
qreg q[1];
unitary q[0];
"""
Expand All @@ -367,9 +367,9 @@ def test_multiple_unitary_qasm(self):
expected = re.compile(
r"""OPENQASM 2.0;
include "qelib1.inc";
gate unitary q0 { u3\(0,0,0\) q0; }
gate (?P<u1>unitary_[0-9]*) q0 { u3\(pi,-pi/2,pi/2\) q0; }
gate (?P<u2>unitary_[0-9]*) q0 { u3\(0,pi/2,pi/2\) q0; }
gate unitary q0 { u\(0,0,0\) q0; }
gate (?P<u1>unitary_[0-9]*) q0 { u\(pi,-pi/2,pi/2\) q0; }
gate (?P<u2>unitary_[0-9]*) q0 { u\(0,pi/2,pi/2\) q0; }
gate custom q0 { (?P=u2) q0; }
qreg q\[2\];
unitary q\[0\];
Expand Down
4 changes: 2 additions & 2 deletions test/python/circuit/test_unitary.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_qasm_unitary_only_one_def(self):
expected_qasm = (
"OPENQASM 2.0;\n"
'include "qelib1.inc";\n'
"gate unitary q0 { u3(0,0,0) q0; }\n"
"gate unitary q0 { u(0,0,0) q0; }\n"
"qreg q0[2];\ncreg c0[1];\n"
"x q0[0];\n"
"unitary q0[0];\n"
Expand All @@ -241,7 +241,7 @@ def test_qasm_unitary_twice(self):
expected_qasm = (
"OPENQASM 2.0;\n"
'include "qelib1.inc";\n'
"gate unitary q0 { u3(0,0,0) q0; }\n"
"gate unitary q0 { u(0,0,0) q0; }\n"
"qreg q0[2];\ncreg c0[1];\n"
"x q0[0];\n"
"unitary q0[0];\n"
Expand Down

0 comments on commit 77801c8

Please sign in to comment.