Skip to content

Commit

Permalink
Fix C3SXGate to_matrix method (#12742)
Browse files Browse the repository at this point in the history
* Fix c3sx matrix

* Apply Jake's suggestion
  • Loading branch information
ElePT committed Jul 9, 2024
1 parent 4fe9dbc commit d86f995
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 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 @@ -22,6 +22,7 @@
from qiskit._accelerate.circuit import StandardGate

_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 @@ -571,6 +572,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
4 changes: 0 additions & 4 deletions test/python/circuit/test_rust_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

SKIP_LIST = {"rx", "ry", "ecr"}
CUSTOM_NAME_MAPPING = {"mcx": C3XGate()}
MATRIX_SKIP_LIST = {"c3sx"}


class TestRustGateEquivalence(QiskitTestCase):
Expand Down Expand Up @@ -141,9 +140,6 @@ def test_matrix(self):
"""Test matrices are the same in rust space."""
for name, gate_class in self.standard_gates.items():
standard_gate = getattr(gate_class, "_standard_gate", None)
if name in MATRIX_SKIP_LIST:
# to_matrix not defined for type
continue
if standard_gate is None:
# gate is not in rust yet
continue
Expand Down

0 comments on commit d86f995

Please sign in to comment.