Skip to content

Commit

Permalink
Fix C3SXGate to_matrix method (backport #12742) (#12745)
Browse files Browse the repository at this point in the history
* Fix `C3SXGate` `to_matrix` method (#12742)

* Fix c3sx matrix

* Apply Jake's suggestion

(cherry picked from commit d86f995)

# Conflicts:
#	test/python/circuit/test_rust_equivalence.py

* Remove rust test

---------

Co-authored-by: Elena Peña Tapia <[email protected]>
Co-authored-by: Elena Peña Tapia <[email protected]>
  • Loading branch information
3 people authored Jul 9, 2024
1 parent 0989210 commit 6b57b78
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qiskit/circuit/library/standard_gates/x.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/fix-c3sx-gate-matrix-050cf9f9ac3b2b82.yaml
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions test/python/circuit/test_controlled_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 6b57b78

Please sign in to comment.