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

Fixes circuit_to_instruction() method for circuits containing single bit conditions #6537

Merged
merged 12 commits into from
Jul 15, 2021
13 changes: 11 additions & 2 deletions qiskit/converters/circuit_to_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from qiskit.exceptions import QiskitError
from qiskit.circuit.instruction import Instruction
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.classicalregister import ClassicalRegister
from qiskit.circuit.classicalregister import ClassicalRegister, Clbit


def circuit_to_instruction(circuit, parameter_map=None, equivalence_library=None, label=None):
Expand Down Expand Up @@ -113,7 +113,16 @@ def circuit_to_instruction(circuit, parameter_map=None, equivalence_library=None
condition = rule[0].condition
if condition:
reg, val = condition
if reg.size == c.size:
if isinstance(reg, Clbit):
idx = 0
for creg in circuit.cregs:
if reg not in creg:
idx += creg.size
else:
cond_reg = creg
break
rule[0].condition = (c[idx + list(cond_reg).index(reg)], val)
elif reg.size == c.size:
rule[0].condition = (c, val)
else:
raise QiskitError(
Expand Down
8 changes: 7 additions & 1 deletion qiskit/visualization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qiskit.quantum_info.operators.symplectic import PauliTable, SparsePauliOp
from qiskit.visualization.exceptions import VisualizationError
from qiskit.circuit import Measure, ControlledGate, Gate, Instruction, Delay, BooleanExpression
from qiskit.circuit import Clbit
from qiskit.circuit.tools import pi_check
from qiskit.exceptions import MissingOptionalLibraryError

Expand Down Expand Up @@ -298,6 +299,7 @@ def __init__(self, dag, justification, measure_map, reverse_bits):
self.qubits = dag.qubits
self.justification = justification
self.measure_map = measure_map
self.cregs = [self.dag.cregs[reg] for reg in self.dag.cregs]
self.reverse_bits = reverse_bits

if self.justification == "left":
Expand Down Expand Up @@ -347,7 +349,11 @@ def slide_from_left(self, node, index):
last_insertable_index = -1
index_stop = -1
if node.op.condition:
index_stop = self.measure_map[node.op.condition[0]]
if isinstance(node.op.condition[0], Clbit):
cond_reg = [creg for creg in self.cregs if node.op.condition[0] in creg]
index_stop = self.measure_map[cond_reg[0]]
else:
index_stop = self.measure_map[node.op.condition[0]]
elif node.cargs:
for carg in node.cargs:
try:
Expand Down
24 changes: 24 additions & 0 deletions test/python/converters/test_circuit_to_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ def test_flatten_circuit_registers(self):
self.assertEqual(inst.definition[1][1], [q[7]])
self.assertEqual(inst.definition[1][2], [c[4]])

def test_flatten_registers_of_circuit_single_bit_cond(self):
"""Check correct mapping of registers gates conditioned on single classical bits."""
qr1 = QuantumRegister(2, "qr1")
qr2 = QuantumRegister(3, "qr2")
cr1 = ClassicalRegister(3, "cr1")
cr2 = ClassicalRegister(3, "cr2")
circ = QuantumCircuit(qr1, qr2, cr1, cr2)
circ.h(qr1[0]).c_if(cr1[1], True)
circ.h(qr2[1]).c_if(cr2[0], False)
circ.cx(qr1[1], qr2[2]).c_if(cr2[2], True)
circ.measure(qr2[2], cr2[0])

inst = circuit_to_instruction(circ)
q = QuantumRegister(5, "q")
c = ClassicalRegister(6, "c")

self.assertEqual(inst.definition[0][1], [q[0]])
self.assertEqual(inst.definition[1][1], [q[3]])
self.assertEqual(inst.definition[2][1], [q[1], q[4]])

self.assertEqual(inst.definition[0][0].condition, (c[1], True))
self.assertEqual(inst.definition[1][0].condition, (c[3], False))
self.assertEqual(inst.definition[2][0].condition, (c[5], True))

def test_flatten_parameters(self):
"""Verify parameters from circuit are moved to instruction.params"""
qr = QuantumRegister(3, "qr")
Expand Down