Skip to content

Commit a28a564

Browse files
committed
Fix Split2QUnitaries dag manipulation
This commit fixes the dag handling to do the 1q unitary insertion. Previously the dag manipulation was being done manually using the insert_node_on_in_edges() rustworkx method. However as the original node had 2 incoming edges for each qubit this caused the dag after running the pass to become corrupted. Each of the new 1q unitary nodes would end up with 2 incident edges and they would be in a sequence. This would result in later passes not being able to correctly understand the state of the circuit correctly. This was causing the unit tests to fail. This commit fixes this by just using `substitute_node_with_dag()` to handle the node substition, while doing it manually to avoid the overhead of checking is probably possible, the case where a unitary is the product of two 1q gates is not very common so optimizing it isn't super critical.
1 parent 1bdf860 commit a28a564

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

qiskit/transpiler/passes/optimization/split_2q_unitaries.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,22 @@ def run(self, dag: DAGCircuit):
4848
decomp._inner_decomposition.specialization
4949
== TwoQubitWeylDecomposition._specializations.IdEquiv
5050
):
51+
new_dag = DAGCircuit()
52+
new_dag.add_qubits(node.qargs)
53+
5154
ur = decomp.K1r
5255
ur_node = DAGOpNode.from_instruction(
53-
CircuitInstruction(UnitaryGate(ur), qubits=(node.qargs[0],)), dag=dag
56+
CircuitInstruction(UnitaryGate(ur), qubits=(node.qargs[0],)), dag=new_dag
5457
)
55-
ur_node._node_id = dag._multi_graph.add_node(ur_node)
56-
dag._increment_op("unitary")
57-
dag._multi_graph.insert_node_on_in_edges(ur_node._node_id, node._node_id)
5858

5959
ul = decomp.K1l
6060
ul_node = DAGOpNode.from_instruction(
61-
CircuitInstruction(UnitaryGate(ul), qubits=(node.qargs[1],)), dag=dag
61+
CircuitInstruction(UnitaryGate(ul), qubits=(node.qargs[1],)), dag=new_dag
6262
)
63-
ul_node._node_id = dag._multi_graph.add_node(ul_node)
64-
dag._increment_op("unitary")
65-
dag._multi_graph.insert_node_on_in_edges(ul_node._node_id, node._node_id)
66-
67-
dag.global_phase += decomp.global_phase
68-
dag.remove_op_node(node)
63+
new_dag._apply_op_node_back(ur_node)
64+
new_dag._apply_op_node_back(ul_node)
65+
new_dag.global_phase = decomp.global_phase
66+
dag.substitute_node_with_dag(node, new_dag)
6967
elif (
7068
decomp._inner_decomposition.specialization
7169
== TwoQubitWeylDecomposition._specializations.SWAPEquiv

test/python/transpiler/test_split_2q_unitaries.py

-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ def test_almost_identity(self):
163163
"""Test that the pass handles QFT correctly."""
164164
qc = QuantumCircuit(2)
165165
qc.cp(pi * 2 ** -(26), 0, 1)
166-
print(Operator(qc).data)
167166
pm = PassManager()
168167
pm.append(Collect2qBlocks())
169168
pm.append(ConsolidateBlocks())

0 commit comments

Comments
 (0)