Skip to content

Commit

Permalink
Alternative implementation to #5002
Browse files Browse the repository at this point in the history
Co-authored-by: faisaldebouni <faisal@megoapps.com>
  • Loading branch information
Luciano Bello and faisaldebouni committed Oct 13, 2020
1 parent 6facb2f commit 11de2b3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/basis/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:
# TODO: allow choosing among multiple decomposition rules
rule = node.op.definition.data

if len(rule) == 1 and len(node.qargs) == len(rule[0][1]):
if len(rule) == 1 and len(node.qargs) == len(rule[0][1]) == 1:
dag.substitute_node(node, rule[0][0], inplace=True)
else:
decomposition = circuit_to_dag(node.op.definition)
Expand Down
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
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

0 comments on commit 11de2b3

Please sign in to comment.