diff --git a/qiskit/quantum_info/operators/dihedral/dihedral_circuits.py b/qiskit/quantum_info/operators/dihedral/dihedral_circuits.py index 394fe1c094a9..2506fd0f5992 100644 --- a/qiskit/quantum_info/operators/dihedral/dihedral_circuits.py +++ b/qiskit/quantum_info/operators/dihedral/dihedral_circuits.py @@ -17,6 +17,8 @@ from qiskit.exceptions import QiskitError from qiskit.circuit import QuantumCircuit +from qiskit.circuit.barrier import Barrier +from qiskit.circuit.delay import Delay def _append_circuit(elem, circuit, qargs=None): @@ -35,6 +37,9 @@ def _append_circuit(elem, circuit, qargs=None): if qargs is None: qargs = list(range(elem.num_qubits)) + if isinstance(circuit, (Barrier, Delay)): + return elem + if isinstance(circuit, QuantumCircuit): gate = circuit.to_instruction() else: @@ -81,6 +86,8 @@ def _append_circuit(elem, circuit, qargs=None): } for instr, qregs, _ in gate.definition: + if isinstance(instr, (Barrier, Delay)): + continue # Get the integer position of the flat register new_qubits = [qargs[bit_indices[tup]] for tup in qregs] diff --git a/qiskit/quantum_info/operators/symplectic/base_pauli.py b/qiskit/quantum_info/operators/symplectic/base_pauli.py index 9d97c56d9af2..ae0f3556e26c 100644 --- a/qiskit/quantum_info/operators/symplectic/base_pauli.py +++ b/qiskit/quantum_info/operators/symplectic/base_pauli.py @@ -20,6 +20,7 @@ from qiskit.circuit import QuantumCircuit from qiskit.circuit.barrier import Barrier +from qiskit.circuit.delay import Delay from qiskit.exceptions import QiskitError from qiskit.quantum_info.operators.base_operator import BaseOperator from qiskit.quantum_info.operators.mixins import AdjointMixin, MultiplyMixin @@ -507,7 +508,7 @@ def _append_circuit(self, circuit, qargs=None): Raises: QiskitError: if input gate cannot be decomposed into Clifford gates. """ - if isinstance(circuit, Barrier): + if isinstance(circuit, (Barrier, Delay)): return self if qargs is None: diff --git a/qiskit/quantum_info/operators/symplectic/clifford_circuits.py b/qiskit/quantum_info/operators/symplectic/clifford_circuits.py index 847725f441c7..513ca3a5c8a3 100644 --- a/qiskit/quantum_info/operators/symplectic/clifford_circuits.py +++ b/qiskit/quantum_info/operators/symplectic/clifford_circuits.py @@ -16,6 +16,7 @@ from qiskit.exceptions import QiskitError from qiskit.circuit import QuantumCircuit from qiskit.circuit.barrier import Barrier +from qiskit.circuit.delay import Delay def _append_circuit(clifford, circuit, qargs=None): @@ -32,7 +33,7 @@ def _append_circuit(clifford, circuit, qargs=None): Raises: QiskitError: if input gate cannot be decomposed into Clifford gates. """ - if isinstance(circuit, Barrier): + if isinstance(circuit, (Barrier, Delay)): return clifford if qargs is None: diff --git a/qiskit/quantum_info/operators/symplectic/pauli.py b/qiskit/quantum_info/operators/symplectic/pauli.py index 9334731fb872..6d4c85459309 100644 --- a/qiskit/quantum_info/operators/symplectic/pauli.py +++ b/qiskit/quantum_info/operators/symplectic/pauli.py @@ -22,6 +22,7 @@ from qiskit.circuit import Instruction, QuantumCircuit from qiskit.circuit.barrier import Barrier +from qiskit.circuit.delay import Delay from qiskit.circuit.library.generalized_gates import PauliGate from qiskit.circuit.library.standard_gates import IGate, XGate, YGate, ZGate from qiskit.exceptions import QiskitError @@ -681,7 +682,7 @@ def _from_circuit(cls, instr): raise QiskitError( f"Cannot apply instruction with classical registers: {dinstr.name}" ) - if not isinstance(dinstr, Barrier): + if not isinstance(dinstr, (Barrier, Delay)): next_instr = BasePauli(*cls._from_circuit(dinstr)) if next_instr is not None: qargs = [tup.index for tup in qregs] diff --git a/releasenotes/notes/clifford_delay-be1a835413e2531e.yaml b/releasenotes/notes/clifford_delay-be1a835413e2531e.yaml new file mode 100644 index 000000000000..653acad17063 --- /dev/null +++ b/releasenotes/notes/clifford_delay-be1a835413e2531e.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixes the :class:`.Clifford`, :class:`.Pauli` and :class:`.CNOTDihedral` + operators support initializing from compatible circuits that contain + :class:`~qiskit.circuit.Delay` instructions. These instructions are + treated as identity when converting to operators. diff --git a/test/python/quantum_info/operators/symplectic/test_clifford.py b/test/python/quantum_info/operators/symplectic/test_clifford.py index 84577f76c6b4..035db3b6c9eb 100644 --- a/test/python/quantum_info/operators/symplectic/test_clifford.py +++ b/test/python/quantum_info/operators/symplectic/test_clifford.py @@ -392,6 +392,21 @@ def test_2_qubit_relations(self): cliff = _append_circuit(cliff, "sdg", [0]) self.assertEqual(cliff, cliff1) + def test_barrier_delay_sim(self): + """Test barrier and delay instructions can be simulated""" + target_circ = QuantumCircuit(2) + target_circ.h(0) + target_circ.cx(0, 1) + target = Clifford(target_circ) + + circ = QuantumCircuit(2) + circ.h(0) + circ.delay(100, 0) + circ.barrier([0, 1]) + circ.cx(0, 1) + value = Clifford(circ) + self.assertEqual(value, target) + @ddt class TestCliffordSynthesis(QiskitTestCase): diff --git a/test/python/quantum_info/operators/symplectic/test_pauli.py b/test/python/quantum_info/operators/symplectic/test_pauli.py index c57ea1408f17..e7aed73dee77 100644 --- a/test/python/quantum_info/operators/symplectic/test_pauli.py +++ b/test/python/quantum_info/operators/symplectic/test_pauli.py @@ -21,6 +21,7 @@ import numpy as np from ddt import ddt, data, unpack +from qiskit import QuantumCircuit from qiskit.exceptions import QiskitError from qiskit.circuit.library import ( IGate, @@ -413,6 +414,21 @@ def test_evolve_clifford_qargs(self): self.assertEqual(value, value_h) self.assertEqual(value_inv, value_s) + def test_barrier_delay_sim(self): + """Test barrier and delay instructions can be simulated""" + target_circ = QuantumCircuit(2) + target_circ.x(0) + target_circ.y(1) + target = Pauli(target_circ) + + circ = QuantumCircuit(2) + circ.x(0) + circ.delay(100, 0) + circ.barrier([0, 1]) + circ.y(1) + value = Pauli(circ) + self.assertEqual(value, target) + if __name__ == "__main__": unittest.main() diff --git a/test/python/quantum_info/operators/test_dihedral.py b/test/python/quantum_info/operators/test_dihedral.py index 74c02144a7be..736bcec8d564 100644 --- a/test/python/quantum_info/operators/test_dihedral.py +++ b/test/python/quantum_info/operators/test_dihedral.py @@ -716,6 +716,21 @@ def test_init_from_pauli(self): target = Operator(elem) self.assertTrue(value.equiv(target), "Error: Pauli operator is not the same.") + def test_barrier_delay_sim(self): + """Test barrier and delay instructions can be simulated""" + target_circ = QuantumCircuit(2) + target_circ.x(0) + target_circ.cx(0, 1) + target = CNOTDihedral(target_circ) + + circ = QuantumCircuit(2) + circ.x(0) + circ.delay(100, 0) + circ.barrier([0, 1]) + circ.cx(0, 1) + value = CNOTDihedral(circ) + self.assertEqual(value, target) + if __name__ == "__main__": unittest.main()