Skip to content

Commit

Permalink
Fixes Clifford, Pauli, CNOTDihedral to handle Delay (#7850)
Browse files Browse the repository at this point in the history
* Fixes Clifford,Pauli,CNOTDihedreal to handle Delay

Fixes the Clifford, Pauli and CNOTDihedreal operator classes to handle conversion of circuits containing Delay instructions. These instructions are ignored (treated as identity) when converting to operators.

* black

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 9c91b6e)
  • Loading branch information
chriseclectic authored and mergify-bot committed Apr 1, 2022
1 parent a2d13f5 commit 53b3ace
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
7 changes: 7 additions & 0 deletions qiskit/quantum_info/operators/dihedral/dihedral_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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]

Expand Down
3 changes: 2 additions & 1 deletion qiskit/quantum_info/operators/symplectic/base_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion qiskit/quantum_info/operators/symplectic/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/clifford_delay-be1a835413e2531e.yaml
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions test/python/quantum_info/operators/symplectic/test_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions test/python/quantum_info/operators/symplectic/test_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
15 changes: 15 additions & 0 deletions test/python/quantum_info/operators/test_dihedral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 53b3ace

Please sign in to comment.