Closed kdk closed 4 years ago
The minimal code which seems to reproduce the bug is,
OPENQASM 2.0;
include "qelib1.inc";
qreg q1[2];
creg c1[1];
if(c1==1) cx q1[0], q1[1];
During transpilation the dag seems to become invalid, when running _check_condition
over nodes, in consolidate_blocks
. It seems consolidate_blocks
relies on being able to create and simulate a circuit with a classical condition but no classical register.
A workaround would be for circuit_to_instruction
to change,
if condition:
-> if condition and instruction.num_clbits > 0:
although it seems strange for circuit_to_instruction to have to check both conditions.
@kdk @ewinston The following diff seems to take care of the problem and passes all the auto-tests. If this seems like a reasonable approach, let me know and I'll do a PR.
--- a/qiskit/transpiler/passes/optimization/consolidate_blocks.py
+++ b/qiskit/transpiler/passes/optimization/consolidate_blocks.py
@@ -16,7 +16,7 @@
"""Replace each block of consecutive gates by a single Unitary node."""
-from qiskit.circuit import QuantumRegister, QuantumCircuit
+from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.dagcircuit import DAGCircuit
from qiskit.quantum_info.operators import Operator
from qiskit.quantum_info.synthesis import TwoQubitBasisDecomposer
@@ -112,12 +112,19 @@ class ConsolidateBlocks(TransformationPass):
else:
# find the qubits involved in this block
block_qargs = set()
+ block_cargs = set()
for nd in block:
block_qargs |= set(nd.qargs)
+ if nd.condition:
+ block_cargs |= set(nd.condition[0])
# convert block to a sub-circuit, then simulate unitary and add
- block_width = len(block_qargs)
- q = QuantumRegister(block_width)
- subcirc = QuantumCircuit(q)
+ q = QuantumRegister(len(block_qargs))
+ # if condition in node, add clbits to circuit
+ if len(block_cargs) > 0:
+ c = ClassicalRegister(len(block_cargs))
+ subcirc = QuantumCircuit(q, c)
+ else:
+ subcirc = QuantumCircuit(q)
block_index_map = self._block_qargs_to_indices(block_qargs,
global_index_map)
basis_count = 0
Thanks @enavarro51 . I agree that looks like a reasonable fix. Can you add a test and open a PR?
Will do. Is a reno required?
Will do. Is a reno required?
Since this fixes a bug from a previous release, a reno would be great.
Information
What is the current behavior?
From https://travis-ci.com/github/Qiskit/qiskit-terra/jobs/358424742#L3190 , in some cases, the transpiler is generating conditional gates without the clbits on which they are conditioned. This was noticed in (but not necessarily caused by) code added in #4622 ( https://github.com/Qiskit/qiskit-terra/pull/4622/files#diff-dd64919667465e6a3c40b2a4ed00f8e5R117 ) when building instructions from circuits with conditional gates.
Steps to reproduce the problem
Transpile the QASM from the below stack trace at
optimization_level=3
targetingFakeJohannesburg
.What is the expected behavior?
Suggested solutions