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

Substitute a DAG node with circuit (substitute_node_with_circuit) #5227

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
19 changes: 19 additions & 0 deletions qiskit/dagcircuit/dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,25 @@ def topological_op_nodes(self):
"""
return (nd for nd in self.topological_nodes() if nd.type == 'op')

def substitute_node_with_circuit(self, node, input_circuit, wires=None):
"""Replace one node with circuit.

Args:
node (DAGNode): node to substitute
input_circuit (QuantumCircuit): circuit that will substitute the node
wires (list[Bit]): gives an order for (qu)bits
in the input circuit. This order gets matched to the node wires
by qargs first, then cargs, then conditions.
"""
from qiskit.converters import circuit_to_dag # pylint: disable=cyclic-import

circuit_data = input_circuit.data
if len(circuit_data) == 1 and len(node.qargs) == len(circuit_data[0][1]) == 1:
self.substitute_node(node, circuit_data[0][0], inplace=True)
else:
decomposition = circuit_to_dag(input_circuit)
self.substitute_node_with_dag(node, decomposition, wires)
1ucian0 marked this conversation as resolved.
Show resolved Hide resolved

def substitute_node_with_dag(self, node, input_dag, wires=None):
"""Replace one node with dag.

Expand Down
9 changes: 2 additions & 7 deletions qiskit/transpiler/passes/basis/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from qiskit.circuit.gate import Gate
from qiskit.transpiler.basepasses import TransformationPass
from qiskit.dagcircuit.dagcircuit import DAGCircuit
from qiskit.converters.circuit_to_dag import circuit_to_dag


class Decompose(TransformationPass):
Expand Down Expand Up @@ -49,11 +48,7 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:
if node.op.definition.global_phase:
dag.global_phase += node.op.definition.global_phase
# TODO: allow choosing among multiple decomposition rules
rule = node.op.definition.data

if len(rule) == 1 and len(node.qargs) == len(rule[0][1]):
dag.substitute_node(node, rule[0][0], inplace=True)
else:
decomposition = circuit_to_dag(node.op.definition)
dag.substitute_node_with_dag(node, decomposition)
dag.substitute_node_with_circuit(node, node.op.definition)

return dag
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/basis/unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def run(self, dag):
# original gate, in which case substitute_node will raise. Fall back
# to substitute_node_with_dag if an the width of the definition is
# different that the width of the node.
while rule and len(rule) == 1 and len(node.qargs) == len(rule[0][1]):
while rule and len(rule) == 1 and len(node.qargs) == len(rule[0][1]) == 1:
if rule[0][0].name in self.basis:
if node.op.definition and node.op.definition.global_phase:
dag.global_phase += node.op.definition.global_phase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
A bug that was causing registers to be mismapped when unrolling/decomposing
a gate defined with only one 2-qubit operation. Now the decomposition will reflect
how a gate is defined.
16 changes: 16 additions & 0 deletions test/python/transpiler/test_decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ def test_decompose_oversized_instruction(self):

self.assertEqual(qc1, output)

def test_decomposition_preserves_qregs_order(self):
"""Test decomposing a gate preserves it's definition registers order"""
qr = QuantumRegister(2, 'qr1')
qc = QuantumCircuit(qr)
qc.cx(1, 0)
gate = qc.to_gate()

qr2 = QuantumRegister(2, 'qr2')
qc2 = QuantumCircuit(qr2)
qc2.append(gate, qr2)

expected = QuantumCircuit(qr2)
expected.cx(1, 0)

self.assertEqual(qc2.decompose(), expected)

def test_decompose_global_phase_1q(self):
"""Test decomposition of circuit with global phase"""
qc = QuantumCircuit(1)
Expand Down
52 changes: 52 additions & 0 deletions test/python/transpiler/test_unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,58 @@ def test_unrolling_parameterized_composite_gates(self):

self.assertEqual(circuit_to_dag(expected), out_dag)

def test_unrolling_preserves_qregs_order(self):
"""Test unrolling a gate preseveres it's definition registers order"""
qr = QuantumRegister(2, 'qr1')
qc = QuantumCircuit(qr)
qc.cx(1, 0)
gate = qc.to_gate()

qr2 = QuantumRegister(2, 'qr2')
qc2 = QuantumCircuit(qr2)
qc2.append(gate, qr2)

dag = circuit_to_dag(qc2)
out_dag = Unroller(['cx']).run(dag)

expected = QuantumCircuit(qr2)
expected.cx(1, 0)

self.assertEqual(circuit_to_dag(expected), out_dag)

def test_unrolling_nested_gates_preserves_qregs_order(self):
"""Test unrolling a nested gate preseveres it's definition registers order."""
qr = QuantumRegister(2, 'qr1')
qc = QuantumCircuit(qr)
qc.cx(1, 0)
gate_level_1 = qc.to_gate()

qr2 = QuantumRegister(2, 'qr2')
qc2 = QuantumCircuit(qr2)
qc2.append(gate_level_1, [1, 0])
qc2.cu1(pi, 1, 0)
gate_level_2 = qc2.to_gate()

qr3 = QuantumRegister(2, 'qr3')
qc3 = QuantumCircuit(qr3)
qc3.append(gate_level_2, [1, 0])
qc3.cu3(pi, pi, pi, 1, 0)
gate_level_3 = qc3.to_gate()

qr4 = QuantumRegister(2, 'qr4')
qc4 = QuantumCircuit(qr4)
qc4.append(gate_level_3, [0, 1])

dag = circuit_to_dag(qc4)
out_dag = Unroller(['cx', 'cu1', 'cu3']).run(dag)

expected = QuantumCircuit(qr4)
expected.cx(1, 0)
expected.cu1(pi, 0, 1)
expected.cu3(pi, pi, pi, 1, 0)

self.assertEqual(circuit_to_dag(expected), out_dag)


class TestUnrollAllInstructions(QiskitTestCase):
"""Test unrolling a circuit containing all standard instructions."""
Expand Down