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_load_qasm_text(self):
"""Test load_qasm_text and get_circuit.
If all is correct we should get the qasm file loaded from the string
Previusly:
Libraries:
from qiskit import QuantumProgram
"""
QP_program = QuantumProgram()
QASM_string = "// A simple 8 qubit example\nOPENQASM 2.0;\n"
QASM_string += "include \"qelib1.inc\";\nqreg a[4];\n"
QASM_string += "qreg b[4];\ncreg c[4];\ncreg d[4];\nh a;\ncx a, b;\n"
QASM_string += "barrier a;\nbarrier b;\nmeasure a[0]->c[0];\n"
QASM_string += "measure a[1]->c[1];\nmeasure a[2]->c[2];\n"
QASM_string += "measure a[3]->c[3];\nmeasure b[0]->d[0];\n"
QASM_string += "measure b[1]->d[1];\nmeasure b[2]->d[2];\n"
QASM_string += "measure b[3]->d[3];"
name = QP_program.load_qasm_text(QASM_string, verbose=False)
result = QP_program.get_circuit(name)
to_check = result.qasm()
# print(to_check)
self.assertEqual(len(to_check), 554)
# looping over all the qubits
for j in range(number_of_qubits):
# making the Pauli for each j fill it in from the
# end first
element = (k // (4 ** j)) % 4
if element == 1:
x[j] = True
elif element == 2:
z[j] = True
x[j] = True
elif element == 3:
z[j] = True
temp_set.append(Pauli(z, x))
return temp_set
else:
raise QiskitError("Only support 'weight' or 'tensor' cases "
"but you have {}.".format(case))
raise QiskitError("Only support number of qubits is less than 5")
def _stinespring_to_operator(data, input_dim, output_dim):
"""Transform Stinespring representation to Operator representation."""
del input_dim # unused
trace_dim = data[0].shape[0] // output_dim
if data[1] is not None or trace_dim != 1:
raise QiskitError(
'Channel cannot be converted to Operator representation')
return data[0]
num_qubits = operator.num_qubits
last_idx = num_qubits - 1
mid_idx = num_qubits // 2 - 1
sq_list = [mid_idx, last_idx]
# build symmetries, sq_paulis, cliffords:
symmetries, sq_paulis, cliffords = [], [], []
for idx in sq_list:
pauli_str = ['I'] * num_qubits
pauli_str[idx] = 'Z'
z_sym = Pauli.from_label(''.join(pauli_str)[::-1])
symmetries.append(z_sym)
pauli_str[idx] = 'X'
sq_pauli = Pauli.from_label(''.join(pauli_str)[::-1])
sq_paulis.append(sq_pauli)
clifford = WeightedPauliOperator(paulis=[[1. / np.sqrt(2), z_sym], [1. / np.sqrt(2), sq_pauli]])
cliffords.append(clifford)
return cls.taper(operator, symmetries, cliffords, sq_list, tapering_values)
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)