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_inverse_instruction_with_measure(self):
"""test inverting instruction with measure fails"""
q = QuantumRegister(4)
c = ClassicalRegister(4)
circ = QuantumCircuit(q, c, name='circ')
circ.t(q[1])
circ.u3(0.1, 0.2, -0.2, q[0])
circ.barrier()
circ.measure(q[0], c[0])
inst = circ.to_instruction()
self.assertRaises(CircuitError, inst.inverse)
def test_dag_get_qubits(self):
"""get_qubits() method """
dag = DAGCircuit()
dag.add_qreg(QuantumRegister(1, 'qr1'))
dag.add_qreg(QuantumRegister(1, 'qr10'))
dag.add_qreg(QuantumRegister(1, 'qr0'))
dag.add_qreg(QuantumRegister(1, 'qr3'))
dag.add_qreg(QuantumRegister(1, 'qr4'))
dag.add_qreg(QuantumRegister(1, 'qr6'))
self.assertListEqual(dag.qubits(), [QuantumRegister(1, 'qr1')[0],
QuantumRegister(1, 'qr10')[0],
QuantumRegister(1, 'qr0')[0],
QuantumRegister(1, 'qr3')[0],
QuantumRegister(1, 'qr4')[0],
QuantumRegister(1, 'qr6')[0]])
def test_apply_cx_to_non_register(self):
"""test applying ccx to non-register raises"""
qr = QuantumRegister(10)
cr = ClassicalRegister(10)
qc = QuantumCircuit(qr, cr)
self.assertRaises(CircuitError, qc.cx, qc[0:2], qc[2:4])
def test_pop_gate(self):
"""Verify removing a gate via circuit.data.pop."""
qr = QuantumRegister(2)
qc = QuantumCircuit(qr)
qc.h(0)
qc.cx(0, 1)
qc.h(1)
last_h = qc.data.pop()
expected_qc = QuantumCircuit(qr)
expected_qc.h(0)
expected_qc.cx(0, 1)
self.assertEqual(qc, expected_qc)
self.assertEqual(last_h, (HGate(), [qr[1]], []))
def test_no_broadcast(self):
"""See https://github.com/Qiskit/qiskit-terra/issues/2777
When creating custom instructions, do not broadcast parameters"""
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
subcircuit = QuantumCircuit(qr, cr, name='subcircuit')
subcircuit.x(qr[0])
subcircuit.h(qr[1])
subcircuit.measure(qr[0], cr[0])
subcircuit.measure(qr[1], cr[1])
inst = subcircuit.to_instruction()
circuit = QuantumCircuit(qr, cr, name='circuit')
circuit.append(inst, qr[:], cr[:])
self.assertEqual(circuit.qregs, [qr])
self.assertEqual(circuit.cregs, [cr])
self.assertEqual(circuit.qubits, [qr[0], qr[1]])
self.assertEqual(circuit.clbits, [cr[0], cr[1]])
def test_4_args_custom_gate_trivial_expansion(self):
"""test 'expansion' of 4 args in custom gate.
See https://github.com/Qiskit/qiskit-terra/issues/2508"""
qr = QuantumRegister(4)
circ = QuantumCircuit(qr)
circ.append(Gate("mcx", 4, []), [qr[0], qr[1], qr[2], qr[3]])
self.assertEqual(len(circ.data), 1)
(gate, qargs, _) = circ.data[0]
self.assertEqual(gate.name, 'mcx')
self.assertEqual(len(qargs), 4)
def test_layout_repr(self):
"""Layout repr reproduces layout"""
qr = QuantumRegister(5, 'qr')
layout = Layout({qr[0]: 2,
qr[1]: 4,
qr[2]: 3,
qr[3]: 0,
qr[4]: 1,
})
repr_layout = eval(layout.__repr__(), {'Qubit': Qubit, # pylint: disable=eval-used
'QuantumRegister': QuantumRegister,
'Layout': Layout})
self.assertDictEqual(layout._p2v, repr_layout._p2v)
self.assertDictEqual(layout._v2p, repr_layout._v2p)
def test_layout_from_intlist_duplicated(self):
"""If the intlist contains duplicated ints, fail.
virtual physical
q1_0 -> 4
q2_0 -> 6 -- This is
q2_1 -> 6 -- not allowed
"""
qr1 = QuantumRegister(1, 'qr1')
qr2 = QuantumRegister(2, 'qr2')
intlist_layout = [4, 6, 6]
with self.assertRaises(LayoutError):
_ = Layout.from_intlist(intlist_layout, qr1, qr2)
# Find swap circuit to preceed to each layer of input circuit
layout = initial_layout.copy()
# Construct an empty DAGCircuit with one qreg "q"
# and the same set of cregs as the input circuit
dagcircuit_output = DAGCircuit()
dagcircuit_output.name = dag.name
dagcircuit_output.add_qreg(QuantumRegister(self.coupling_map.size(), "q"))
for creg in dag.cregs.values():
dagcircuit_output.add_creg(creg)
# Make a trivial wire mapping between the subcircuits
# returned by swap_mapper_layer_update and the circuit
# we are building
identity_wire_map = {}
q = QuantumRegister(self.coupling_map.size(), 'q')
for j in range(self.coupling_map.size()):
identity_wire_map[q[j]] = q[j]
for creg in dag.cregs.values():
for j in range(creg.size):
identity_wire_map[creg[j]] = creg[j]
first_layer = True # True until first layer is output
# Iterate over layers
for i, layer in enumerate(layerlist):
# Attempt to find a permutation for this layer
success_flag, best_circ, best_d, best_layout, trivial_flag \
= self.layer_permutation(layer["partition"], layout, qubit_subset)
# If this fails, try one gate at a time in this layer
def _do_checks(flags, qr_variables, qb_target, qr_ancillae, circuit):
# check flags
if flags is None:
flags = [1 for i in range(len(qr_variables))]
else:
if len(flags) > len(qr_variables):
raise AquaError('`flags` cannot be longer than `qr_variables`.')
# check variables
# TODO: improve the check
if isinstance(qr_variables, QuantumRegister) or isinstance(qr_variables, list):
variable_qubits = [qb for qb, i in zip(qr_variables, flags) if not i == 0]
else:
raise ValueError('A QuantumRegister or list of qubits is expected for variables.')
# check target
if isinstance(qb_target, Qubit):
target_qubit = qb_target
else:
raise ValueError('A single qubit is expected for the target.')
# check ancilla
if qr_ancillae is None:
ancillary_qubits = []
elif isinstance(qr_ancillae, QuantumRegister):
ancillary_qubits = [qb for qb in qr_ancillae]
elif isinstance(qr_ancillae, list):