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_accred_circuits(self):
""" Test circuit generation """
seed_accreditation = 208723512
n_qb = 4
v = 10
# Create a Quantum Register with n_qb qubits.
q_reg = QuantumRegister(n_qb, 'q')
# Create a Classical Register with n_qb bits.
c_reg = ClassicalRegister(n_qb, 's')
# Create a Quantum Circuit acting on the q register
target_circuit = QuantumCircuit(q_reg, c_reg)
# dummy circuit
target_circuit.h(0)
target_circuit.h(1)
target_circuit.h(2)
target_circuit.h(3)
target_circuit.cz(0, 1)
target_circuit.cz(0, 2)
target_circuit.cz(0, 3)
target_circuit.h(1)
target_circuit.h(2)
target_circuit.h(3)
target_circuit.measure(q_reg, c_reg)
# make trap circuits
def _bell_circuit():
qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.cx(qr[0], qr[1])
qc.measure(qr, cr)
return qc
def test_transpile_two(self):
"""Test transpile to circuits.
If all correct some should exists.
"""
backend = BasicAer.get_backend('qasm_simulator')
qubit_reg = QuantumRegister(2)
clbit_reg = ClassicalRegister(2)
qubit_reg2 = QuantumRegister(2)
clbit_reg2 = ClassicalRegister(2)
qc = QuantumCircuit(qubit_reg, clbit_reg, name="bell")
qc.h(qubit_reg[0])
qc.cx(qubit_reg[0], qubit_reg[1])
qc.measure(qubit_reg, clbit_reg)
qc_extra = QuantumCircuit(qubit_reg, qubit_reg2, clbit_reg, clbit_reg2, name="extra")
qc_extra.measure(qubit_reg, clbit_reg)
circuits = transpile([qc, qc_extra], backend)
self.assertIsInstance(circuits[0], QuantumCircuit)
self.assertIsInstance(circuits[1], QuantumCircuit)
p.ccx(a, b, c)
def unmajority(p, a, b, c):
"""Unmajority gate."""
p.ccx(a, b, c)
p.cx(c, a)
p.cx(a, b)
n = 4
a = qiskit.QuantumRegister("a", n)
b = qiskit.QuantumRegister("b", n)
cin = qiskit.QuantumRegister("cin", 1)
cout = qiskit.QuantumRegister("cout", 1)
ans = qiskit.ClassicalRegister("ans", n + 1)
# Build subcircuit to add a to b, storing result in b
adder_subcircuit = qiskit.QuantumCircuit(cin, a, b, cout)
majority(adder_subcircuit, cin[0], b[0], a[0])
for j in range(n - 1):
majority(adder_subcircuit, a[j], b[j + 1], a[j + 1])
adder_subcircuit.cx(a[n - 1], cout[0])
for j in reversed(range(n - 1)):
unmajority(adder_subcircuit, a[j], b[j + 1], a[j + 1])
unmajority(adder_subcircuit, cin[0], b[0], a[0])
# Build the adder example
qc = qiskit.QuantumCircuit(cin, a, b, cout, ans)
qc.x(a[0]) # Set input a = 0...0001
qc.x(b) # Set input b = 1...1111
qc += adder_subcircuit
def test_decompose_conditional(self):
"""Test decompose a 3-qubit gate with a conditional.
"""
qr = QuantumRegister(3, 'qr')
cr = ClassicalRegister(1, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 0)
dag = circuit_to_dag(circuit)
pass_ = Unroll3qOrMore()
after_dag = pass_.run(dag)
op_nodes = after_dag.op_nodes()
self.assertEqual(len(op_nodes), 15)
for node in op_nodes:
self.assertIn(node.name, ['h', 't', 'tdg', 'cx'])
self.assertEqual(node.condition, (cr, 0))
def delay_measure_circuit(self):
"""Test circuit that allows measure delay optimization"""
qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(2, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.x(0)
circuit.measure(0, 0)
circuit.barrier([0, 1])
circuit.x(1)
circuit.measure(0, 1)
return circuit
def setUp(self):
qr = QuantumRegister(1)
cr = ClassicalRegister(1)
self._qc1 = QuantumCircuit(qr, cr, name='qc1')
self._qc2 = QuantumCircuit(qr, cr, name='qc2')
self._qc1.measure(qr[0], cr[0])
self.backend = BasicAer.get_backend('qasm_simulator')
self._result1 = execute(self._qc1, self.backend).result()
def cx_gate_circuits_deterministic(final_measure=True):
"""CX-gate test circuits with deterministic counts."""
circuits = []
qr = QuantumRegister(2)
if final_measure:
cr = ClassicalRegister(2)
regs = (qr, cr)
else:
regs = (qr, )
# CX01, |00> state
circuit = QuantumCircuit(*regs)
circuit.cx(qr[0], qr[1])
if final_measure:
circuit.barrier(qr)
circuit.measure(qr, cr)
circuits.append(circuit)
# CX10, |00> state
circuit = QuantumCircuit(*regs)
circuit.cx(qr[1], qr[0])
if final_measure:
return job.result().get_counts()
else:
# Execute the program in the simulator.
print("Running on the simulator.")
job = qiskit.execute(program, qiskit.Aer.get_backend('qasm_simulator'), shots=shots)
return job.result().get_counts()
run.isInit = False
#
# Example 1: Measure 2 qubits in their initial state, all zeros.
#
# Setup qubits.
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
program = QuantumCircuit(qr, cr);
# Measure the value of the qubits in their initial state, they should be all zeros.
program.measure(qr, cr);
# Execute the program.
print(run(program, type))
#
# Example 2: Create a Bell state (|00> + |11>), (|00> - |11>), (|01> + |10>), (|01> - |10>): Entangle 2 qubits, with the first in superposition (existing as 0 and 1 simulataneously or 50% chance of either value) and measure the results, they should be half 00 and half 11.
#
# Setup qubits.
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
program = QuantumCircuit(qr, cr);
def construct_circuit(self, x, theta, measurement=False):
"""
Construct circuit based on data and parameters in variational form.
Args:
x (numpy.ndarray): 1-D array with D dimension
theta ([numpy.ndarray]): list of 1-D array, parameters sets for variational form
measurement (bool): flag to add measurement
Returns:
QuantumCircuit: the circuit
"""
qr = QuantumRegister(self._num_qubits, name='q')
cr = ClassicalRegister(self._num_qubits, name='c')
qc = QuantumCircuit(qr, cr)
# encode the data with custom initialization (instead of feature map)
custominput = Custom(self._num_qubits, state_vector=x)
qc += custominput.construct_circuit('circuit', qr)
qc += self._var_form.construct_circuit(theta, qr)
if measurement:
qc.barrier(qr)
qc.measure(qr, cr)
return qc