Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, gates_to_cancel: List[Union[Gate, Tuple[Gate, Gate]]]):
raise TranspilerError(
"Too many or too few inputs: {}. Only two are allowed.".format(gates)
)
elif gates[0] != gates[1].inverse():
if gates[0] != gates[1].inverse():
raise TranspilerError(
"Gate {} and {} are not inverse.".format(gates[0].name, gates[1].name)
)
Expand Down
22 changes: 12 additions & 10 deletions test/python/transpiler/test_inverse_cancellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@


class TestInverseCancellation(QiskitTestCase):
"""Test the InverseCancellation transpiler pass."""

def test_basic_self_inverse(self):
"""Test that a single self-inverse gate as input can be cancelled."""
qc = QuantumCircuit(2, 2)
Expand Down Expand Up @@ -83,14 +85,6 @@ def test_non_inverse_do_not_cancel(self):
self.assertIn("rx", gates_after)
self.assertEqual(gates_after["rx"], 2)

def test_non_inverse_raise_error(self):
"""Test that non-inverse gate inputs raise an error."""
qc = QuantumCircuit(2, 2)
qc.rx(np.pi / 2, 0)
qc.rx(np.pi / 4, 0)
with self.assertRaises(TranspilerError):
pass_ = InverseCancellation([RXGate(0.5)])

def test_non_consecutive_gates(self):
"""Test that only consecutive gates cancel."""
qc = QuantumCircuit(2, 2)
Expand Down Expand Up @@ -131,21 +125,29 @@ def test_self_inverse_on_different_qubits(self):
gates_after = new_circ.count_ops()
self.assertNotIn("h", gates_after)

def test_non_inverse_raise_error(self):
"""Test that non-inverse gate inputs raise an error."""
qc = QuantumCircuit(2, 2)
qc.rx(np.pi / 2, 0)
qc.rx(np.pi / 4, 0)
with self.assertRaises(TranspilerError):
InverseCancellation([RXGate(0.5)])

def test_non_gate_inverse_raise_error(self):
"""Test that non-inverse gate inputs raise an error."""
qc = QuantumCircuit(2, 2)
qc.rx(np.pi / 4, 0)
qc.rx(np.pi / 4, 0)
with self.assertRaises(TranspilerError):
pass_ = InverseCancellation([(RXGate(np.pi / 4))])
InverseCancellation([(RXGate(np.pi / 4))])

def test_string_gate_error(self):
"""Test that when gate is passed as a string an error is raised."""
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.h(0)
with self.assertRaises(TranspilerError):
pass_ = InverseCancellation(["h"])
InverseCancellation(["h"])

def test_consecutive_self_inverse_h_x_gate(self):
"""Test that only consecutive self-inverse gates cancel."""
Expand Down