Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_schedule_length3(self):
"""Testing with repeated calls to run"""
bprop = create_fake_machine()
crosstalk_prop = {}
crosstalk_prop[(0, 1)] = {(2, 3): 0.2}
crosstalk_prop[(2, 3)] = {(0, 1): 0.05, (4, 5): 0.05}
crosstalk_prop[(4, 5)] = {(2, 3): 0.05}
crosstalk_prop[(1, 2)] = {(3, 4): 0.05}
crosstalk_prop[(3, 4)] = {(1, 2): 0.05}
qr = QuantumRegister(6, 'q')
circuit = QuantumCircuit(qr)
circuit.cx(qr[0], qr[1])
circuit.cx(qr[2], qr[3])
mapping = [0, 1, 2, 3, 4, 5]
layout = Layout({qr[i]: mapping[i] for i in range(6)})
new_circ = transpile(circuit, initial_layout=layout, basis_gates=['u1', 'u2', 'u3', 'cx'])
dag = circuit_to_dag(new_circ)
pass_ = CrosstalkAdaptiveSchedule(bprop, crosstalk_prop)
scheduled_dag1 = pass_.run(dag)
scheduled_dag2 = pass_.run(dag)
self.assertEqual(scheduled_dag1.depth(), 3)
self.assertEqual(scheduled_dag2.depth(), 3)
def test_can_add_gates_into_free_space(self):
"""The scheduler does some time bookkeeping to know when qubits are free to be
scheduled. Make sure this works for qubits that are used in the future. This was
a bug, uncovered by this example:
q0 = - - - - |X|
q1 = |X| |u2| |X|
In ALAP scheduling, the next operation on qubit 0 would be added at t=0 rather
than immediately before the X gate.
"""
qr = QuantumRegister(2)
qc = QuantumCircuit(qr)
for i in range(2):
qc.u2(0, 0, [qr[i]])
qc.u1(3.14, [qr[i]])
qc.u2(0, 0, [qr[i]])
sched = schedule(qc, self.backend, method="alap")
expected = Schedule(
self.cmd_def.get('u2', [0], 0, 0),
self.cmd_def.get('u2', [1], 0, 0),
(28, self.cmd_def.get('u1', [0], 3.14)),
(28, self.cmd_def.get('u1', [1], 3.14)),
(28, self.cmd_def.get('u2', [0], 0, 0)),
(28, self.cmd_def.get('u2', [1], 0, 0)))
self.assertEqual(sched.instructions, expected.instructions)
def s_gate_circuits_deterministic(final_measure=True):
"""S-gate test circuits with deterministic counts."""
circuits = []
qr = QuantumRegister(1)
if final_measure:
cr = ClassicalRegister(1)
regs = (qr, cr)
else:
regs = (qr, )
# S
circuit = QuantumCircuit(*regs)
circuit.s(qr)
if final_measure:
circuit.barrier(qr)
circuit.measure(qr, cr)
circuits.append(circuit)
# S.X
circuit = QuantumCircuit(*regs)
circuit.x(qr)
circuit.barrier(qr)
circuit.s(qr)
if final_measure:
circuit.barrier(qr)
circuit.measure(qr, cr)
circuits.append(circuit)
def test_flatten_circuit_registers(self):
"""Check correct flattening"""
qr1 = QuantumRegister(4, 'qr1')
qr2 = QuantumRegister(3, 'qr2')
qr3 = QuantumRegister(3, 'qr3')
cr1 = ClassicalRegister(4, 'cr1')
cr2 = ClassicalRegister(1, 'cr2')
circ = QuantumCircuit(qr1, qr2, qr3, cr1, cr2)
circ.cx(qr1[1], qr2[2])
circ.measure(qr3[0], cr2[0])
inst = circuit_to_instruction(circ)
q = QuantumRegister(10, 'q')
c = ClassicalRegister(5, 'c')
self.assertEqual(inst.definition[0][1], [q[1], q[6]])
self.assertEqual(inst.definition[1][1], [q[7]])
self.assertEqual(inst.definition[1][2], [c[4]])
""" Remove a single Cu1Gate
qr0:-CU1-m--- qr0:--m---
| | |
qr1:--.--|-m- ==> qr1:--|-m-
| | | |
cr0:-----.-.- cr0:--.-.-
"""
qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(1, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.cu1(0.1, qr[0], qr[1])
circuit.measure(qr[0], cr[0])
circuit.measure(qr[1], cr[0])
dag = circuit_to_dag(circuit)
expected = QuantumCircuit(qr, cr)
expected.measure(qr[0], cr[0])
expected.measure(qr[1], cr[0])
pass_ = RemoveDiagonalGatesBeforeMeasure()
after = pass_.run(dag)
self.assertEqual(circuit_to_dag(expected), after)
# assure every time the operator from the original one
qubit_op = copy.deepcopy(qubit_op_temp)
if expansion_mode == 'suzuki':
self.log.debug(
'With expansion order {}:'.format(expansion_order))
state_out_matrix = qubit_op.evolve(
state_in=state_in.construct_circuit('vector'),
evo_time=evo_time,
evo_mode='matrix',
num_time_slices=num_time_slices,
expansion_mode=expansion_mode,
expansion_order=expansion_order
)
quantum_registers = QuantumRegister(qubit_op.num_qubits, name='q')
qc = QuantumCircuit(quantum_registers)
qc += state_in.construct_circuit(
'circuit', quantum_registers)
qc += qubit_op.evolve(
evo_time=evo_time,
evo_mode='circuit',
num_time_slices=num_time_slices,
quantum_registers=quantum_registers,
expansion_mode=expansion_mode,
expansion_order=expansion_order,
)
job = q_execute(qc, BasicAer.get_backend('statevector_simulator'))
state_out_circuit = np.asarray(
job.result().get_statevector(qc, decimals=16))
self.log.debug('The fidelity between exact and matrix: {}'.format(
state_fidelity(state_out_exact, state_out_matrix)
def test_circuit_initialize(self):
"""Test initialize on wires.
"""
init_vector = [0.5, 0.5, 0.5, 0.5]
qreg01 = QuantumRegister(2)
qreg23 = QuantumRegister(2)
circuit = QuantumCircuit(qreg01, qreg23)
circuit.initialize(init_vector, range(1, 3))
expected = QuantumCircuit(qreg01, qreg23)
expected.initialize(init_vector, [qreg01[1], qreg23[0]])
self.assertEqual(circuit, expected)
def test_single_gates_omitted(self):
"""Test if single qubit gates are omitted."""
coupling_map = [[0, 1], [1, 0], [1, 2], [1, 3], [2, 1], [3, 1], [3, 4], [4, 3]]
qr = QuantumRegister(5, 'q')
cr = ClassicalRegister(5, 'c')
circuit = QuantumCircuit(qr, cr)
circuit.cx(qr[0], qr[4])
circuit.cx(qr[1], qr[2])
circuit.u3(1, 1.5, 0.7, qr[3])
expected = QuantumCircuit(qr, cr)
expected.cx(qr[1], qr[2])
expected.u3(1, 1.5, 0.7, qr[3])
expected.swap(qr[0], qr[1])
expected.swap(qr[3], qr[4])
expected.cx(qr[1], qr[3])
expected_dag = circuit_to_dag(expected)
stochastic = StochasticSwap(CouplingMap(coupling_map), seed=0)
after = PassManager(stochastic).run(circuit)
after = circuit_to_dag(after)
def test_conditional_operation(self):
"""Test conditional operation.
"""
qr = QuantumRegister(4)
cr = ClassicalRegister(4)
circ = QuantumCircuit(qr, cr)
circ.x(qr[0])
circ.x(qr[2])
circ.measure(qr[0], cr[0])
circ.x(qr[0]).c_if(cr, 1)
qobj = compile(circ, self._remote_backend)
result_remote = self._remote_backend.run(qobj).result()
result_local = self._local_backend.run(qobj).result()
self.assertDictAlmostEqual(result_remote.get_counts(circ),
result_local.get_counts(circ), delta=100)
def construct_circuit(self, params=None):
"""
Construct generator circuit.
Args:
params (numpy.ndarray): parameters which should be used to run the generator,
if None use self._params
Returns:
Instruction: construct the quantum circuit and return as gate
"""
q = QuantumRegister(sum(self._num_qubits), name='q')
qc = QuantumCircuit(q)
if params is None:
self.generator_circuit.build(qc=qc, q=q)
else:
generator_circuit_copy = deepcopy(self.generator_circuit)
generator_circuit_copy.params = params
generator_circuit_copy.build(qc=qc, q=q)
# return qc.copy(name='qc')
return qc.to_instruction()