def test_swap_between_qregs(self): """ Adding a swap affecting different qregs qr0_0:------- qr1_0:--(+)-- | qr1_1:---.--- Coupling map: [1]--[0]--[2] qr0_0:--X--.--- | | qr1_0:--|-(+)-- | qr1_1:--X------ """ coupling = Coupling({0: [1, 2]}) qr0 = QuantumRegister(1, 'qr0') qr1 = QuantumRegister(2, 'qr1') circuit = QuantumCircuit(qr0, qr1) circuit.cx(qr1[0], qr1[1]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr0, qr1) expected.swap(qr1[1], qr0[0]) expected.cx(qr1[1], qr0[0]) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(circuit_to_dag(expected), after)
def test_a_single_swap(self): """ Adding a swap q0:------- q1:--(+)-- | q2:---.--- Coupling map: [1]--[0]--[2] q0:--X---.--- | | q1:--X---|--- | q2:-----(+)-- """ coupling = Coupling({0: [1, 2]}) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cx(qr[1], qr[2]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr) expected.swap(qr[1], qr[0]) expected.cx(qr[0], qr[2]) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(circuit_to_dag(expected), after)
def test_keep_layout(self): """After a swap, the following gates also change the wires. qr0:---.---[H]-- | qr1:---|-------- | qr2:--(+)------- Coupling map: [0]--[1]--[2] qr0:--X----------- | qr1:--X---.--[H]-- | qr2:-----(+)------ """ coupling = Coupling({1: [0, 2]}) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cx(qr[0], qr[2]) circuit.h(qr[0]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr) expected.swap(qr[0], qr[1]) expected.cx(qr[1], qr[2]) expected.h(qr[1]) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(circuit_to_dag(expected), after)
def test_far_swap_with_gate_the_middle(self): """ A far swap with a gate in the middle. qr0:--(+)-------.-- | | qr1:---|--------|-- | qr2:---|--------|-- | | qr3:---.--[H]--(+)- Coupling map: [0]--[1]--[2]--[3] qr0:-------(+)-------.--- | | qr1:-----X--.--[H]--(+)-- | qr2:--X--X--------------- | qr3:--X------------------ """ coupling = Coupling({0: [1], 1: [2], 2: [3]}) qr = QuantumRegister(4, 'qr') circuit = QuantumCircuit(qr) circuit.cx(qr[3], qr[0]) circuit.h(qr[3]) circuit.cx(qr[0], qr[3]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr) expected.swap(qr[3], qr[2]) expected.swap(qr[2], qr[1]) expected.cx(qr[1], qr[0]) expected.h(qr[1]) expected.cx(qr[0], qr[1]) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(circuit_to_dag(expected), after)
def test_far_swap(self): """ A far swap that affects coming CXs. qr0:--(+)---.-- | | qr1:---|----|-- | | qr2:---|----|-- | | qr3:---.---(+)- Coupling map: [0]--[1]--[2]--[3] qr0:--X-------------- | qr1:--X--X----------- | qr2:-----X--(+)---.-- | | qr3:---------.---(+)- """ coupling = Coupling({0: [1], 1: [2], 2: [3]}) qr = QuantumRegister(4, 'q') circuit = QuantumCircuit(qr) circuit.cx(qr[0], qr[3]) circuit.cx(qr[3], qr[0]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr) expected.swap(qr[0], qr[1]) expected.swap(qr[1], qr[2]) expected.cx(qr[2], qr[3]) expected.cx(qr[3], qr[2]) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(circuit_to_dag(expected), after)
def test_trivial_case(self): """No need to have any swap, the CX are distance 1 to each other q0:--(+)-[U]-(+)- | | q1:---.-------|-- | q2:-----------.-- Coupling map: [1]--[0]--[2] """ coupling = Coupling({0: [1, 2]}) qr = QuantumRegister(3, 'q') circuit = QuantumCircuit(qr) circuit.cx(qr[0], qr[1]) circuit.h(qr[0]) circuit.cx(qr[0], qr[2]) dag = circuit_to_dag(circuit) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(dag, after)
def test_trivial_in_same_layer(self): """ No need to have any swap, two CXs distance 1 to each other, in the same layer q0:--(+)-- | q1:---.--- q2:--(+)-- | q3:---.--- Coupling map: [0]--[1]--[2]--[3] """ coupling = Coupling({0: [1], 1: [2], 2: [3]}) qr = QuantumRegister(4, 'q') circuit = QuantumCircuit(qr) circuit.cx(qr[2], qr[3]) circuit.cx(qr[0], qr[1]) dag = circuit_to_dag(circuit) pass_ = BasicMapper(coupling) after = pass_.run(dag) self.assertEqual(dag, after)