Skip to content
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

Fix C3SXGate to_matrix method #12742

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 `gate.to_matrix()` was called.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can link :meth:`.Gate.to_matrix` to get the proper cross-ref.

ElePT marked this conversation as resolved.
Show resolved Hide resolved
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
Loading