Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def swap_gate_circuits_nondeterministic(final_measure=True):
"""SWAP-gate test circuits with non-deterministic counts."""
circuits = []
qr = QuantumRegister(3)
if final_measure:
cr = ClassicalRegister(3)
regs = (qr, cr)
else:
regs = (qr, )
# initial state as |10+>
# Swap(0,1).(X^I^H), Permutation (0,1,2) -> (1,0,2)
circuit = QuantumCircuit(*regs)
circuit.h(qr[0])
circuit.barrier(qr)
circuit.x(qr[2])
circuit.barrier(qr)
circuit.swap(qr[0], qr[1])
if final_measure:
circuit.barrier(qr)
def test_schedule_multi(self):
"""Test scheduling multiple circuits at once."""
q = QuantumRegister(2)
c = ClassicalRegister(2)
qc0 = QuantumCircuit(q, c)
qc0.cx(q[0], q[1])
qc1 = QuantumCircuit(q, c)
qc1.cx(q[0], q[1])
schedules = schedule([qc0, qc1], self.backend)
expected_insts = schedule(qc0, self.backend).instructions
for actual, expected in zip(schedules[0].instructions, expected_insts):
self.assertEqual(actual[0], expected[0])
self.assertEqual(actual[1].command, expected[1].command)
self.assertEqual(actual[1].channels, expected[1].channels)
def test_6q_circuit_20q_coupling(self):
"""Test finds dense 5q corner in 20q coupling map.
"""
qr0 = QuantumRegister(3, 'q0')
qr1 = QuantumRegister(3, 'q1')
circuit = QuantumCircuit(qr0, qr1)
circuit.cx(qr0[0], qr1[2])
circuit.cx(qr1[1], qr0[2])
dag = circuit_to_dag(circuit)
pass_ = DenseLayout(CouplingMap(self.cmap20))
pass_.run(dag)
layout = pass_.property_set['layout']
self.assertEqual(layout[qr0[0]], 11)
self.assertEqual(layout[qr0[1]], 10)
self.assertEqual(layout[qr0[2]], 6)
self.assertEqual(layout[qr1[0]], 5)
self.assertEqual(layout[qr1[1]], 1)
self.assertEqual(layout[qr1[2]], 0)
def test_qasm_reset_measure(self):
"""counts from a qasm program with measure and reset in the middle"""
qr = qiskit.QuantumRegister(3)
cr = qiskit.ClassicalRegister(3)
circuit = qiskit.QuantumCircuit(qr, cr)
circuit.h(qr[0])
circuit.cx(qr[0], qr[1])
circuit.reset(qr[0])
circuit.cx(qr[1], qr[2])
circuit.t(qr)
circuit.measure(qr[1], cr[1])
circuit.h(qr[2])
circuit.measure(qr[2], cr[2])
sim_cpp = qiskit.providers.aer.QasmSimulator()
sim_py = qiskit.providers.basicaer.QasmSimulatorPy()
shots = 1000
result_cpp = execute(circuit, sim_cpp, shots=shots, seed=1).result()
result_py = execute(circuit, sim_py, shots=shots, seed=1).result()
snapshot_dir=None)
self.qgan.seed = 7
# Set quantum instance to run the quantum generator
self.qi_statevector = QuantumInstance(backend=BasicAer.get_backend('statevector_simulator'),
seed_simulator=2,
seed_transpiler=2)
self.qi_qasm = QuantumInstance(backend=BasicAer.get_backend('qasm_simulator'),
shots=1000,
seed_simulator=2,
seed_transpiler=2)
# Set entangler map
entangler_map = [[0, 1]]
# Set an initial state for the generator circuit
init_dist = UniformDistribution(sum(num_qubits), low=self._bounds[0], high=self._bounds[1])
q = QuantumRegister(sum(num_qubits), name='q')
qc = QuantumCircuit(q)
init_dist.build(qc, q)
init_distribution = Custom(num_qubits=sum(num_qubits), circuit=qc)
# Set variational form
var_form = RY(sum(num_qubits),
depth=1,
initial_state=init_distribution,
entangler_map=entangler_map,
entanglement_gate='cz')
# Set generator's initial parameters
init_params = aqua_globals.random.rand(var_form._num_parameters) * 2 * 1e-2
# Set generator circuit
g_circuit = UnivariateVariationalDistribution(sum(num_qubits), var_form, init_params,
low=self._bounds[0],
high=self._bounds[1])
# Set quantum generator
| |
qr2:---|----|--
| |
qr3:---.---(+)-
Coupling map: [0]--[1]--[2]--[3]
qr0:-x------------
|
qr1:-x--(+)---.---
| |
qr2:-x---.---(+)--
|
qr3:-x------------
"""
coupling = CouplingMap([[0, 1], [1, 2], [2, 3]])
qr = QuantumRegister(4, 'q')
circuit = QuantumCircuit(qr)
circuit.cx(qr[0], qr[3])
circuit.cx(qr[3], qr[0])
dag = circuit_to_dag(circuit)
pass_ = StochasticSwap(coupling, 20, 13)
after = pass_.run(dag)
expected = QuantumCircuit(qr)
expected.swap(qr[0], qr[1])
expected.swap(qr[2], qr[3])
expected.cx(qr[1], qr[2])
expected.cx(qr[2], qr[1])
self.assertEqual(circuit_to_dag(expected), after)
def test_gate_x(self):
shots = 100
qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr, name='test_gate_x')
qc.x(qr[0])
qc.measure(qr, cr)
qobj = qiskit._compiler.compile([qc], pq_simulator, shots=shots)
q_job = QuantumJob(qobj, pq_simulator, preformatted=True,
resources={'max_credits': qobj['config']['max_credits']})
job = pq_simulator.run(q_job)
result_pq = job.result(timeout=30)
self.assertEqual(result_pq.get_counts(result_pq.get_names()[0]),
{'1': shots})
def test_run_async_device(self, qe_token, qe_url):
IBMQ.enable_account(qe_token, qe_url)
backends = IBMQ.backends(simulator=False)
backend = least_busy(backends)
self.log.info('submitting to backend %s', backend.name())
num_qubits = 5
qr = QuantumRegister(num_qubits, 'qr')
cr = ClassicalRegister(num_qubits, 'cr')
qc = QuantumCircuit(qr, cr)
for i in range(num_qubits-1):
qc.cx(qr[i], qr[i+1])
qc.measure(qr, cr)
qobj = compile(qc, backend)
num_jobs = 3
job_array = [backend.run(qobj) for _ in range(num_jobs)]
time.sleep(3) # give time for jobs to start (better way?)
job_status = [job.status() for job in job_array]
num_init = sum([status is JobStatus.INITIALIZING for status in job_status])
num_queued = sum([status is JobStatus.QUEUED for status in job_status])
num_running = sum([status is JobStatus.RUNNING for status in job_status])
num_done = sum([status is JobStatus.DONE for status in job_status])
num_error = sum([status is JobStatus.ERROR for status in job_status])
self.log.info('number of currently initializing jobs: %d/%d',
def test_cannot_optimize(self):
""" Cannot optimize when swap is not at the end in all of the successors
qr0:--X-----m--
| |
qr1:--X-[H]-|--
|
cr0:--------.--
"""
qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(1, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.swap(qr[0], qr[1])
circuit.h(qr[1])
circuit.measure(qr[0], cr[0])
dag = circuit_to_dag(circuit)
pass_ = OptimizeSwapBeforeMeasure()
after = pass_.run(dag)
self.assertEqual(circuit_to_dag(circuit), after)
@staticmethod
def _construct_circuit(x1, x2, num_qubits, feature_map, measurement, circuit_name=None):
if x1.shape[0] != x2.shape[0]:
raise ValueError("x1 and x2 must be the same dimension.")
q = QuantumRegister(num_qubits, 'q')
c = ClassicalRegister(num_qubits, 'c')
qc = QuantumCircuit(q, c, name=circuit_name)
# write input state from sample distribution
qc += feature_map.construct_circuit(x1, q)
qc += feature_map.construct_circuit(x2, q, inverse=True)
if measurement:
qc.barrier(q)
qc.measure(q, c)
return qc