diff --git a/qiskit/circuit/library/standard_gates/x.py b/qiskit/circuit/library/standard_gates/x.py index 7195df90dc98..ddcf93d68b33 100644 --- a/qiskit/circuit/library/standard_gates/x.py +++ b/qiskit/circuit/library/standard_gates/x.py @@ -21,6 +21,7 @@ from qiskit.circuit._utils import _ctrl_state_to_int, with_gate_array, with_controlled_gate_array _X_ARRAY = [[0, 1], [1, 0]] +_SX_ARRAY = [[0.5 + 0.5j, 0.5 - 0.5j], [0.5 - 0.5j, 0.5 + 0.5j]] @with_gate_array(_X_ARRAY) @@ -562,6 +563,7 @@ def __eq__(self, other): return isinstance(other, RCCXGate) +@with_controlled_gate_array(_SX_ARRAY, num_ctrl_qubits=3, cached_states=(7,)) class C3SXGate(SingletonControlledGate): """The 3-qubit controlled sqrt-X gate. diff --git a/releasenotes/notes/fix-c3sx-gate-matrix-050cf9f9ac3b2b82.yaml b/releasenotes/notes/fix-c3sx-gate-matrix-050cf9f9ac3b2b82.yaml new file mode 100644 index 000000000000..accd20de4ae5 --- /dev/null +++ b/releasenotes/notes/fix-c3sx-gate-matrix-050cf9f9ac3b2b82.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed a missing decorator in :class:`.C3SXGate` that made it fail if :meth:`.Gate.to_matrix` was called. + The gate matrix is now return as expected. diff --git a/test/python/circuit/test_controlled_gate.py b/test/python/circuit/test_controlled_gate.py index ced7229415ea..580bd9d4df48 100644 --- a/test/python/circuit/test_controlled_gate.py +++ b/test/python/circuit/test_controlled_gate.py @@ -230,6 +230,12 @@ def test_special_cases_equivalent_to_controlled_base_gate(self): # Ensure that both the array form (if the gate overrides `__array__`) and the # circuit-definition form are tested. self.assertTrue(Operator(special_case_gate).equiv(naive_operator)) + if not isinstance(special_case_gate, (MCXGate, MCPhaseGate, MCU1Gate)): + # Ensure that the to_matrix method yields the same result + np.testing.assert_allclose( + special_case_gate.to_matrix(), naive_operator.to_matrix(), atol=1e-8 + ) + if not isinstance(special_case_gate, CXGate): # CX is treated like a primitive within Terra, and doesn't have a definition. self.assertTrue(Operator(special_case_gate.definition).equiv(naive_operator))