Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
CouplingMap map: [1]--[0]--[2]
q0:--X---.---
| |
q1:--X---|---
|
q2:-----(+)--
"""
coupling = CouplingMap([[0, 1], [0, 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_ = BasicSwap(coupling)
after = pass_.run(dag)
self.assertEqual(circuit_to_dag(expected), after)
def test_depth_one(self):
""" A dag with operations in parallel and depth 1"""
qr = QuantumRegister(2)
circuit = QuantumCircuit(qr)
circuit.h(qr[0])
circuit.h(qr[1])
dag = circuit_to_dag(circuit)
pass_ = Depth()
_ = pass_.run(dag)
self.assertEqual(pass_.property_set['depth'], 1)
def test_commutative_circuit(self):
"""A simple circuit where two CNOTs commute
qr0:----.------------
|
qr1:---(+)-----(+)---
|
qr2:---[H]------.----
"""
qr = QuantumRegister(3, 'qr')
circuit = QuantumCircuit(qr)
circuit.cx(qr[0], qr[1])
circuit.h(qr[2])
circuit.cx(qr[2], qr[1])
dag = circuit_to_dag(circuit)
self.pass_.run(dag)
expected = {'qr[0]': [[1], [7], [2]],
'qr[1]': [[3], [7, 9], [4]],
'qr[2]': [[5], [8], [9], [6]]}
self.assertCommutationSet(self.pset["commutation_set"], expected)
cr0:-----.-- cr0:--.-
"""
qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(1, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.t(qr[0])
circuit.measure(qr[0], cr[0])
dag = circuit_to_dag(circuit)
expected = QuantumCircuit(qr, cr)
expected.measure(qr[0], cr[0])
pass_ = RemoveDiagonalGatesBeforeMeasure()
after = pass_.run(dag)
self.assertEqual(circuit_to_dag(expected), after)
circuit = QuantumCircuit(qr0, qr1, cr0, cr1)
circuit.h(qr0)
circuit.measure(qr0, cr0)
circuit.z(qr1).c_if(cr0, 1)
circuit.measure(qr1, cr1)
expected = QuantumCircuit(qr0, qr1, cr0, cr1)
expected.h(qr0)
expected.measure(qr0, cr0)
expected.z(qr1).c_if(cr0, 1)
expected.barrier(qr1)
expected.measure(qr1, cr1)
pass_ = BarrierBeforeFinalMeasurements()
result = pass_.run(circuit_to_dag(circuit))
self.assertEqual(result, circuit_to_dag(expected))
def test_qobj_to_circuit_with_parameters(self):
"""Check qobj_to_circuit result with a gate that uses parameters."""
qreg1 = QuantumRegister(2)
qreg2 = QuantumRegister(3)
creg1 = ClassicalRegister(2)
creg2 = ClassicalRegister(2)
circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2)
circuit_b.x(qreg1)
circuit_b.h(qreg2)
circuit_b.u2(0.2, 0.57, qreg2[1])
circuit_b.measure(qreg1, creg1)
circuit_b.measure(qreg2[0], creg2[1])
qobj = assemble(circuit_b)
out_circuit = qobj_to_circuits(qobj)
self.assertEqual(circuit_to_dag(out_circuit[0]),
circuit_to_dag(circuit_b))
def test_3q_circuit_5q_coupling(self):
"""Test finds trivial layout for 3q circuit on 5q device.
"""
qr = QuantumRegister(3, 'q')
circuit = QuantumCircuit(qr)
circuit.cx(qr[1], qr[0])
circuit.cx(qr[0], qr[2])
circuit.cx(qr[1], qr[2])
dag = circuit_to_dag(circuit)
pass_ = TrivialLayout(CouplingMap(self.cmap5))
pass_.run(dag)
layout = pass_.property_set['layout']
for i in range(3):
self.assertEqual(layout[qr[i]], i)
"""
qr = QuantumRegister(2, 'qr')
circuit = QuantumCircuit(qr)
circuit.h(qr[0])
circuit.x(qr[0])
circuit.y(qr[0])
circuit.t(qr[0])
circuit.s(qr[0])
circuit.rz(0.5, qr[0])
circuit.u1(0.5, qr[0])
circuit.u2(0.5, 0.6, qr[0])
circuit.u3(0.5, 0.6, 0.7, qr[0])
circuit.cx(qr[0], qr[1])
circuit.cy(qr[0], qr[1])
circuit.cz(qr[0], qr[1])
dag = circuit_to_dag(circuit)
self.pass_.run(dag)
expected = {'qr[0]': [[1],
[5],
[6],
[7],
[8, 9, 10, 11],
[12],
[13],
[14],
[15],
[16],
[2]],
'qr[1]': [[3], [14], [15], [16], [4]]}
self.assertCommutationSet(self.pset["commutation_set"], expected)
def test_qobj_to_circuits_multiple(self):
"""Check that qobj_to_circuits's result with multiple circuits"""
qreg1 = QuantumRegister(2)
qreg2 = QuantumRegister(3)
creg1 = ClassicalRegister(2)
creg2 = ClassicalRegister(2)
circuit_b = QuantumCircuit(qreg1, qreg2, creg1, creg2)
circuit_b.x(qreg1)
circuit_b.h(qreg2)
circuit_b.measure(qreg1, creg1)
circuit_b.measure(qreg2[0], creg2[1])
qobj = assemble([self.circuit, circuit_b])
dag_list = [circuit_to_dag(x) for x in qobj_to_circuits(qobj)]
self.assertEqual(dag_list, [self.dag, circuit_to_dag(circuit_b)])
expected.y(qr[1])
expected.x(qr[0])
expected.swap(qr[1], qr[2])
expected.cx(qr[0], qr[2])
expected.swap(qr[2], qr[3])
expected.cx(qr[1], qr[2])
expected.s(qr[3])
expected.t(qr[1])
expected.h(qr[2])
expected.measure(qr[0], cr[0])
expected.swap(qr[1], qr[2])
expected.cx(qr[3], qr[2])
expected.measure(qr[1], cr[3])
expected.measure(qr[3], cr[1])
expected.measure(qr[2], cr[2])
expected_dag = circuit_to_dag(expected)
# ┌───┐ ┌─┐
# q_0: |0>─────────────┤ X ├──■──┤M├────────────────────────────────────────
# ┌───┐ └───┘ │ └╥┘ ┌───┐ ┌───┐┌─┐
# q_1: |0>─────┤ Y ├─X────────┼───╫───────────■──┤ T ├────────┤ X ├┤M├──────
# ┌───┐└───┘ │ ┌─┴─┐ ║ ┌─┴─┐└───┘┌───┐ └─┬─┘└╥┘┌─┐
# q_2: |0>┤ Z ├──────X──────┤ X ├─╫──X──────┤ X ├─────┤ H ├─X───■───╫─┤M├───
# └───┘ └───┘ ║ │ ┌───┐└───┘ └───┘ │ ║ └╥┘┌─┐
# q_3: |0>────────────────────────╫──X─┤ S ├────────────────X───────╫──╫─┤M├
# ║ └───┘ ║ ║ └╥┘
# c_0: 0 ════════════════════════╩═════════════════════════════════╬══╬══╬═
# ║ ║ ║
# c_1: 0 ══════════════════════════════════════════════════════════╬══╩══╬═
# ║ ║
# c_2: 0 ══════════════════════════════════════════════════════════╩═════╬═
# ║
# c_3: 0 ════════════════════════════════════════════════════════════════╩═