def generate_program(cls, nqbits: int): pr = Program() # TODO extend to nqbits nqbits = 1 to_teleport = pr.qalloc(nqbits) to_teleport_c = pr.calloc(nqbits) epr_pair_a = pr.qalloc() epr_pair_a_c = pr.calloc() epr_pair_b = pr.qalloc() # Prepare EPR pair pr.apply(H, *epr_pair_a) pr.apply(CNOT, *epr_pair_a, *epr_pair_b) # Prepare random state using 3 random rotation around the 3 axis pr.apply(RY(random() * pi), *to_teleport) pr.apply(RX(random() * pi), *to_teleport) pr.apply(RZ(random() * pi), *to_teleport) # Encode pr.apply(CNOT, *to_teleport, *epr_pair_a) pr.apply(H, *to_teleport) # Teleport pr.measure(to_teleport, to_teleport_c) pr.measure(epr_pair_a, epr_pair_a_c) # Decode pr.cc_apply(epr_pair_a_c[0], X, epr_pair_b[0]) pr.cc_apply(to_teleport_c[0], Z, epr_pair_b[0]) return pr, None
def test_sample_2qb(self): qpu = PyLinalg() prog = Program() qbits = prog.qalloc(2) prog.apply(X, qbits[0]) circ = prog.to_circ() obs = Observable(2, pauli_terms=[Term(1., "ZZ", [0, 1])]) job = circ.to_job("OBS", observable=obs) result = qpu.submit(job) self.assertAlmostEqual(result.value, -1) prog = Program() qbits = prog.qalloc(2) prog.apply(X, qbits[0]) prog.apply(X, qbits[1]) circ = prog.to_circ() obs = Observable(2, pauli_terms=[Term(1., "ZZ", [0, 1])]) job = circ.to_job("OBS", observable=obs) result = qpu.submit(job) self.assertAlmostEqual(result.value, 1)
def main(): # _a is for Alice, _b is for Bob, _c is for classical pr = Program() to_teleport = pr.qalloc() epr_pair_a = pr.qalloc() epr_pair_a_c = pr.calloc() to_teleport_c = pr.calloc() epr_pair_b = pr.qalloc() # Prepare EPR pair pr.apply(H, *epr_pair_a) pr.apply(CNOT, *epr_pair_a, *epr_pair_b) # Now Alice has her half of the EPR pair (epr_pair_a) and Bob the other one # (epr_pair_b qubit) # Prepare random state on the qubit(s) to teleport pr.apply(RY(random() * pi), *to_teleport) pr.apply(RX(random() * pi), *to_teleport) pr.apply(RZ(random() * pi), *to_teleport) # At this point we make a copy of the original program. The idea is to show # the state we would obtain if we would stop at this stage, before the # teleportation. pr2 = deepcopy(pr) # We continue with the teleportation circuit # Alice interact her to_teleport_qubit with her half of the EPR pair pr.apply(CNOT, *to_teleport, *epr_pair_a) pr.apply(H, *to_teleport) # ... and then she measures her 2 qubits pr.measure(to_teleport, to_teleport_c) pr.measure(epr_pair_a, epr_pair_a_c) # She then sends her measured qubits to Bob which, depending on their value # being 0 or 1, performs the classically controlled X and Z on his own half of the EPR pair pr.cc_apply(epr_pair_a_c[0], X, epr_pair_b[0]) pr.cc_apply(to_teleport_c[0], Z, epr_pair_b[0]) # circ = pr.to_circ() circ2 = pr2.to_circ() # simulation qpu = PyLinalg() res = qpu.submit(circ.to_job(qubits=[epr_pair_b])) res2 = qpu.submit(circ2.to_job(qubits=[to_teleport])) print("Original state, measured on to_teleport qubit") for sample in res2: # print(f"state {sample.state} with amplitude {sample.amplitude} and probability {sample.probability}") print(f"state {sample.state} with amplitude {sample.probability}") print("Teleported state, measured on ") for sample in res: print(f"state {sample.state} with probability {sample.probability}")
def cirq_to_qlm(circ, sep_measures=False, **kwargs): """ Converts a google cirq circuit to a qlm circuit Args: cirq: the cirq circuit to convert sep_measures: Separates measures from the circuit: - if set to :code:`True` measures won't be included in the resulting circuits, qubits to be measured will be put in a list, the resulting measureless circuit and this list will be returned in a tuple : (resulting_circuit, list_qubits) - if set to :code:`False`, measures will be converted normally (Default set to False) kwargs: these are the options that you would use on a regular to_circ function, to generate a QLM circuit from a PyAQASM program these are added for more flexibility, for advanced users Returns: :code:`tuple` or :class:`~qat.core.Circuit`: If :code:`sep_measures` is set to: - :code:`True`: the result is a tuple composed of a :class:`~qat.core.Circuit` and a list of qubits that should be measured - :code:`False`: the result is a :class:`~qat.core.Circuit` """ # building a qubit map to use correct qubits qubits = ops.QubitOrder.as_qubit_order(ops.QubitOrder.DEFAULT).order_for( circ.all_qubits()) qmap = {qbit: i for i, qbit in enumerate(qubits)} # extracting operations operations = tuple(ops.flatten_op_tree(circ.all_operations())) # pyaqasm initialization prog = Program() qreg = prog.qalloc(len(qubits)) to_measure = [] # building operations for op in operations: qbs = [] for qb in op.qubits: qbs.append(qreg[qmap[qb]]) if (cirq.is_measurement(cast(ops.GateOperation, op)) and sep_measures): to_measure.append(qmap[qb]) if cirq.is_measurement(cast(ops.GateOperation, op)): if not sep_measures: prog.measure(qbs, qbs) elif isinstance(_get_gate(op.gate), str) and _get_gate( op.gate) == "none": continue else: prog.apply(_get_gate(op.gate), qbs) if sep_measures: return prog.to_circ(**kwargs), list(set(to_measure)) return prog.to_circ(**kwargs)
def test1_qiskit_qpu_4states(self): """ In the case of two H gates, 4 states are expected in output. """ nbqubits = 2 prog = Program() qreg = prog.qalloc(nbqubits) creg = prog.calloc(nbqubits) prog.apply(H, qreg[0]) prog.apply(H, qreg[1]) prog.measure(qreg, creg) qlm_circuit = prog.to_circ() qlm_job = qlm_circuit.to_job(nbshots=1024) # no backend is specified qpu = BackendToQPU() result = qpu.submit(qlm_job) string = "\nBackendToQPU with a Hadamard on each qubit " \ + "(expects four different measured states):" LOGGER.debug(string) for entry in result.raw_data: LOGGER.debug("State: %s\t probability: %s", entry.state, entry.probability) self.assertEqual(4, len(result.raw_data))
def test_multiple_measurements(): """ Submit a circuit composed to 2 intermediate measurements """ # Build a program prog = Program() qbits = prog.qalloc(2) cbits = prog.calloc(2) prog.apply(X, qbits[0]) prog.measure(qbits, cbits) prog.apply(CNOT, qbits) prog.measure(qbits, cbits) circ = prog.to_circ() # Submit circuit result = PyLinalg().submit(circ.to_job()) # Check result assert len(result) == 1 sample = result.raw_data[0] assert sample.state.int == 3 # Check intermediate measurements assert len(sample.intermediate_measurements) == 2 assert sample.intermediate_measurements[0].cbits == [True, False] assert sample.intermediate_measurements[1].cbits == [True, True]
def generate_qft_circuit(nbqbits: int, inline: bool = True) -> Circuit: """ Creates a quantum circuit composed of an initialization and a QFT applied on all qubits. Args: nbqbits (int): number of qubits in the circuit inline (bool, optional): True to inline the circuit, False otherwise, default True Returns: Circuit: a quantum circuit containing random gates """ # Initialize program and qregister prog = Program() qbits = prog.qalloc(nbqbits) # Qubits initialization (to have a non |0^n> state) for qbit in qbits: prog.apply(H, qbit) prog.apply(Z, qbit) # Apply QFT on all qubits prog.apply(QFT(nbqbits), qbits) return prog.to_circ(inline=inline)
def test_no_state_modification_circuit(self) -> None: """ We apply Sabre on a circuit which doesn't modify the initial state (|0^n> here) and we verify Sabre circuit modifications don't modify the state. """ for nbqbit in range(min_nbqbit, max_nbqbit): prog = Program() qbits = prog.qalloc(nbqbit) random_angles = [ rd.random() * 2 * np.pi for _ in range(3 * nbqbit) ] for i in range(len(qbits)): prog.apply(RX(random_angles[3 * i]), qbits[i]) prog.apply(RX(random_angles[3 * i + 1]), qbits[i]) prog.apply(RX(random_angles[3 * i + 2]), qbits[i]) prog.apply(QFT(nbqbit), qbits) prog.apply(QFT(nbqbit).dag(), qbits) for i in range(len(qbits)): prog.apply(RX(random_angles[3 * i]).dag(), qbits[i]) prog.apply(RX(random_angles[3 * i + 1]).dag(), qbits[i]) prog.apply(RX(random_angles[3 * i + 2]).dag(), qbits[i]) circuit = prog.to_circ(inline=True) for topology in generate_custom_topologies(nbqbit): qpu = Sabre() | (QuameleonPlugin(topology=topology) | PyLinalg()) result = qpu.submit(circuit.to_job()) assert result.raw_data[0].state.int == 0
def test_balanced(self): max_qubit = 6 for n in range(1, max_qubit + 1): with self.subTest(n=n): # It also creates an additional qubit for output oracle = BalancedOracle(n) count1 = 0 for i in range(2**n): pr = Program() qreg = pr.qalloc(n) qout = pr.qalloc(1) qr_init = initialize_qureg_given_int(i, qreg, False) pr.apply(qr_init, qreg) pr.apply(oracle.generate(), [*qreg, *qout]) # display(pr.to_circ()) # res = self.qpu.submit(pr.to_circ().to_job()) # for sample in res: # print(sample.state, sample.probability) res = self.qpu.submit(pr.to_circ().to_job(qubits=[qout])) self.assertEqual(len(res.raw_data), 1) sample = res.raw_data[0] if sample.state.state == 1: count1 += 1 self.assertEqual(count1, 2**(n - 1))
def test0_default_gates(self): """ Tests all the default QLM gates which conversion in Qiskit are supported. """ prog = Program() qreg = prog.qalloc(5) for gate_op in PYGATES_1QB: prog.apply(gate_op, qreg[0]) for gate_op in PYGATES_2QB: prog.apply(gate_op, qreg[0], qreg[1]) prog.apply(CCNOT, qreg[0], qreg[1], qreg[2]) prog.apply(SWAP.ctrl(), qreg[0], qreg[1], qreg[2]) prog.apply(MS(3.14, 3), qreg[1], qreg[2], qreg[4]) qlm_circuit = prog.to_circ() result = qlm_to_qiskit(qlm_circuit) qiskit_qreg = QuantumRegister(5) qiskit_creg = ClassicalRegister(5) expected = QuantumCircuit(qiskit_qreg, qiskit_creg) for gate_op in qiskit_1qb(expected): gate_op(qiskit_qreg[0]) for gate_op in qiskit_1qb_1prm(expected): gate_op(3.14, qiskit_qreg[0]) for gate_op in qiskit_1qb_2prm(expected): gate_op(3.14, 3.14, qiskit_qreg[0]) for gate_op in qiskit_1qb_3prm(expected): gate_op(3.14, 3.14, 3.14, qiskit_qreg[0]) for gate_op in qiskit_2qb(expected): gate_op(qiskit_qreg[0], qiskit_qreg[1]) for gate_op in qiskit_2qb_1prm(expected): gate_op(3.14, qiskit_qreg[0], qiskit_qreg[1]) expected.ccx(*qiskit_qreg[:3]) expected.cswap(*qiskit_qreg[:3]) # for the MS gate test for i in [1, 2, 4]: for j in [1, 2, 4]: if j > i: expected.rxx(3.14, qiskit_qreg[i], qiskit_qreg[j]) expected.measure(qiskit_qreg, qiskit_creg) LOGGER.debug("qlm_to_qiskit test with standard circuit:") expected_str = print_qiskit(expected) result_str = print_qiskit(result) self.assertEqual(len(result_str), len(expected_str)) for i in range(len(result.data)): r_name, r_params = extract_qiskit(result.data[i])[0:2] e_name, e_params = extract_qiskit(expected.data[i])[0:2] self.assertEqual(r_name, e_name) self.assertEqual(r_params, e_params)
def make_c_control_circuit(): prog = Program() qbits = prog.qalloc(2) cbits = prog.calloc(2) prog.apply(X, qbits[0]) prog.measure(qbits[0], cbits[0]) prog.cc_apply(cbits[0], X, qbits[1]) prog.measure(qbits[1], cbits[1]) return prog.to_circ()
def generate_break(): prog = Program() qbits = prog.qalloc(3) cbits = prog.calloc(3) prog.apply(X, qbits[1]) prog.measure(qbits[0], cbits[0]) prog.measure(qbits[1], cbits[1]) prog.cbreak(cbits[1] & (~cbits[0])) return prog.to_circ()
def generate_teleportation(split_measures: bool): """ Generates a circuit corresponding to the teleportation circuit Args: split_measures (bool): split measures Returns: :class:`~qat.core.Circuit`: generated circuit """ # Init program prog = Program() source = prog.qalloc(1) bell_pair = prog.qalloc(2) cbits = prog.calloc(2) # Init source qubit prog.apply(RX(1.23), source) prog.apply(RZ(4.56), source) # Init Bell pair prog.apply(H, bell_pair[0]) prog.apply(CNOT, bell_pair) # Bell pair measurement between source qubit and bell_pair[0] prog.apply(CNOT, source, bell_pair[0]) prog.apply(H, source) if split_measures: prog.measure(source[0], cbits[0]) prog.measure(bell_pair[0], cbits[1]) else: prog.measure([source[0], bell_pair[0]], cbits) # Classic control prog.cc_apply(cbits[1], X, bell_pair[1]) prog.cc_apply(cbits[0], Z, bell_pair[1]) # Return circuit return prog.to_circ()
def generate_boolean(): prog = Program() qbits = prog.qalloc(3) cbits = prog.calloc(3) prog.apply(X, qbits[1]) prog.measure(qbits[0], cbits[0]) prog.measure(qbits[1], cbits[1]) prog.logic(cbits[2], cbits[1] & ~cbits[0]) prog.cc_apply(cbits[2], X, qbits[2]) prog.apply(X, qbits[0]) return prog.to_circ()
def make_simple_logic(): prog = Program() qbits = prog.qalloc(2) cbits = prog.calloc(5) prog.apply(X, qbits[0]) prog.measure(qbits[0], cbits[0]) prog.measure(qbits[1], cbits[1]) prog.logic(cbits[2], cbits[1] | cbits[0]) prog.logic(cbits[3], cbits[2] & cbits[1]) prog.logic(cbits[4], cbits[0] ^ cbits[1]) return prog.to_circ()
def test4_cannot_measure_observable(self): """ Checks if measuring an Observable raises an error """ prog = Program() qbits = prog.qalloc(1) prog.apply(X, qbits) circ = prog.to_circ() qpu = BackendToQPU(Aer.get_backend('qasm_simulator')) self.assertRaises(QPUException, qpu.submit, circ.to_job("OBS", observable=Observable(1)))
def test_default_gates_and_qbit_reorder(self): aq = AqasmPrinter(MainEngine) eng = AqasmEngine(aq, engine_list=[aq]) qreg1 = eng.allocate_qureg(2) qreg2 = eng.allocate_qureg(1) qreg3 = eng.allocate_qureg(2) for op in gates_1qb: op | qreg2[0] for op in gates_2qb: op | (qreg3[0], qreg1[1]) ControlledGate(ops.Swap, n=1) | (qreg3[1], qreg1[0], qreg2[0]) Toffoli | (qreg3[1], qreg1[0], qreg2[0]) All(Measure) | qreg1 All(Measure) | qreg2 All(Measure) | qreg3 # Generating qlm circuit result = eng.projectq_to_qlm() # Generating equivalent qlm circuit prog = Program() qubits = prog.qalloc(5) cbits = prog.calloc(5) for op in pygates_1qb: prog.apply(op, qubits[2]) for op in pygates_2qb: prog.apply(op, qubits[3], qubits[1]) prog.apply(SWAP, qubits[1], qubits[3]) prog.apply(SWAP.ctrl(), qubits[4], qubits[0], qubits[2]) prog.apply(X.ctrl().ctrl(), qubits[0], qubits[4], qubits[2]) for i in range(5): prog.measure(qubits[i], cbits[i]) expected = prog.to_circ() self.assertEqual(len(result.ops), len(expected.ops)) for i in range(len(result.ops)): res_op = result.ops[i] exp_op = expected.ops[i] if res_op.type == OpType.MEASURE: self.assertEqual(res_op, exp_op) continue result_gate_name, result_gate_params = extract_syntax( result.gateDic[res_op.gate], result.gateDic) # print("got gate {} with params {} on qbits {}" # .format(result_gate_name, result_gate_params, # res_op.qbits)) expected_gate_name, expected_gate_params = extract_syntax( expected.gateDic[exp_op.gate], expected.gateDic) # print("expected gate {} with params {} on qbits {}" # .format(expected_gate_name, expected_gate_params, # exp_op.qbits)) self.assertEqual(expected_gate_name, result_gate_name) self.assertEqual(expected_gate_params, result_gate_params) self.assertEqual(exp_op.qbits, res_op.qbits)
def test1_abstract_gate(self): """ Tests an AbstractGate translation to Qiskit. Only abstract gates defined via a circuit are supported. """ prog = Program() qreg = prog.qalloc(3) routine = QRoutine() for gate_op in PYGATES_1QB: routine.apply(gate_op, [0]) for gate_op in PYGATES_2QB: routine.apply(gate_op, [0, 1]) routine.apply(CCNOT, [0, 1, 2]) routine.apply(SWAP.ctrl(), [0, 1, 2]) prog.apply(routine.box("custom_gate"), qreg) qlm_circuit = prog.to_circ() result = qlm_to_qiskit(qlm_circuit) qiskit_qreg = QuantumRegister(3) qiskit_creg = ClassicalRegister(3) expected = QuantumCircuit(qiskit_qreg, qiskit_creg) for gate_op in qiskit_1qb(expected): gate_op(qiskit_qreg[0]) for gate_op in qiskit_1qb_1prm(expected): gate_op(3.14, qiskit_qreg[0]) for gate_op in qiskit_1qb_2prm(expected): gate_op(3.14, 3.14, qiskit_qreg[0]) for gate_op in qiskit_1qb_3prm(expected): gate_op(3.14, 3.14, 3.14, qiskit_qreg[0]) for gate_op in qiskit_2qb(expected): gate_op(qiskit_qreg[0], qiskit_qreg[1]) for gate_op in qiskit_2qb_1prm(expected): gate_op(3.14, qiskit_qreg[0], qiskit_qreg[1]) expected.ccx(*qiskit_qreg) expected.cswap(*qiskit_qreg) expected.measure(qiskit_qreg, qiskit_creg) LOGGER.debug("qlm_to_qiskit test with a QRoutine:") expected_str = print_qiskit(expected) result_str = print_qiskit(result) self.assertEqual(len(result_str), len(expected_str)) for i in range(len(result.data)): r_name, r_params = extract_qiskit(result.data[i])[0:2] e_name, e_params = extract_qiskit(expected.data[i])[0:2] self.assertEqual(r_name, e_name) self.assertEqual(r_params, e_params)
def test2_abstract_variables(self): """ Tests the translation of abstract variables and ArithExpression into Qiskit via qlm_to_qiskit() """ prog = Program() qubits = prog.qalloc(1) var2 = prog.new_var(float, "param2") var3 = prog.new_var(float, "param3") var4 = 1.0 + 3.14 + var2 - var3 var5 = 1.0 * 3.14 * (var2 + 4.54) * var3 var6 = -var5 * var4 var7 = var4 / (var2 - 7) prog.apply(RX(1.0), qubits[0]) prog.apply(RX(3.14), qubits[0]) prog.apply(RX(var2), qubits[0]) prog.apply(RX(var3), qubits[0]) prog.apply(RX(var4), qubits[0]) prog.apply(RX(var5), qubits[0]) prog.apply(RX(var6), qubits[0]) prog.apply(RX(var7), qubits[0]) qlm_circ = prog.to_circ() qiskit_circ = qlm_to_qiskit(qlm_circ) LOGGER.debug("Parameters gotten:") for gate_op in qiskit_circ.data: for param in gate_op[0]._params: LOGGER.debug(param) qreg = QuantumRegister(1) circ = QuantumCircuit(qreg) param2 = Parameter("param2") param3 = Parameter("param3") param4 = 1.0 + 3.14 + param2 - param3 param5 = 1.0 * 3.14 * (param2 + 4.54) * param3 param6 = -param5 * param4 param7 = param4 / (param2 - 7.0) circ.rx(1.0, 0) circ.rx(3.14, 0) circ.rx(param2, 0) circ.rx(param3, 0) circ.rx(param4, 0) circ.rx(param5, 0) circ.rx(param6, 0) circ.rx(param7, 0) LOGGER.debug("Parameters expected:") for gate_op in circ.data: for param in gate_op[0]._params: LOGGER.debug(param) for gotten, expected in zip(qiskit_circ.data, circ.data): self.assertEqual(str(gotten[0]._params[0]), str(expected[0]._params[0]))
def test_basic(self): with self.assertRaises(QPUException): prog = Program() qbits = prog.qalloc(1) prog.apply(X, qbits) circ = prog.to_circ() obs = Observable(1, pauli_terms=[Term(1., "Z", [0])]) job = circ.to_job("OBS", observable=obs, nbshots=10) qpu = PyLinalg() result = qpu.submit(job)
def test0_asyncqiskit_qpu_2states(self): """ In the case of a H and a CNOT gate, 2 states are expected in output. """ nbqubits = 2 prog = Program() qreg = prog.qalloc(nbqubits) creg = prog.calloc(nbqubits) prog.apply(H, qreg[0]) prog.apply(CNOT, qreg[0], qreg[1]) prog.measure(qreg, creg) qlm_circuit = prog.to_circ() qlm_job1 = qlm_circuit.to_job(nbshots=1024) qlm_job2 = qlm_circuit.to_job(nbshots=1024) batch = Batch(jobs=[qlm_job1, qlm_job2]) # a backend is specified backend = Aer.get_backend('qasm_simulator') qpu = AsyncBackendToQPU(backend) job = qpu.submit(batch) string = "\nAsyncBackendToQPU test with a Hadamard and a CNOT " \ + "(expects two different measured states):" LOGGER.debug(string) LOGGER.debug("ID: %s\t status : %s", job.job_id(), job.status()) time.sleep(0.01) LOGGER.debug("ID: %s\t status : %s", job.job_id(), job.status()) time.sleep(0.2) LOGGER.debug("ID: %s\t status : %s", job.job_id(), job.status()) while job.result() is None: time.sleep(5) results = job.result() for result in results: for entry in result.raw_data: LOGGER.debug("State: %s\t probability: %s", entry.state, entry.probability) self.assertEqual(2, len(result.raw_data)) self.assertTrue( "|00>" in [str(result.raw_data[i].state) for i in range(2)]) self.assertTrue( "|11>" in [str(result.raw_data[i].state) for i in range(2)])
def test_sample_1qb_Y(self): prog = Program() qbits = prog.qalloc(1) prog.apply(H, qbits) prog.apply(PH(-np.pi / 2), qbits) circ = prog.to_circ() obs = Observable(1, pauli_terms=[Term(1., "Y", [0])]) job = circ.to_job("OBS", observable=obs) qpu = PyLinalg() result = qpu.submit(job) self.assertAlmostEqual(result.value, -1) obs = Observable(1, pauli_terms=[Term(18., "Y", [0])]) job = circ.to_job("OBS", observable=obs) result = qpu.submit(job) self.assertAlmostEqual(result.value, -18) prog = Program() qbits = prog.qalloc(1) prog.apply(H, qbits) prog.apply(PH(np.pi / 2), qbits) circ = prog.to_circ() obs = Observable(1, pauli_terms=[Term(1., "Y", [0])]) job = circ.to_job("OBS", observable=obs) result = qpu.submit(job) self.assertAlmostEqual(result.value, 1) obs = Observable(1, pauli_terms=[Term(18., "Y", [0])]) job = circ.to_job("OBS", observable=obs) result = qpu.submit(job) self.assertAlmostEqual(result.value, 18)
def test_submit_job(self): # Create a valid qpu qpu_valid = SimulatedAnnealing( temp_t=TestSimulatedAnnealing.temp_t_valid, n_steps=TestSimulatedAnnealing.n_steps_valid, seed=8017) # Create an Observable Job and check that such Jobs are not dealt with by the qpu observable = Observable(5) job = Job(observable=observable) with pytest.raises(exceptions_types.QPUException): assert result == qpu_valid.submit_job(job) # Create a circuit Job a and check that such Jobs are not dealt with by the qpu from qat.lang.AQASM import Program, H prog = Program() reg = prog.qalloc(1) prog.apply(H, reg) prog.reset(reg) with pytest.raises(exceptions_types.QPUException): assert result == qpu_valid.submit(prog.to_circ().to_job(nbshots=1)) # Create a Job from a Schedule with empty drive and check that such # Jobs are not dealt with by the qpu schedule = Schedule() job = Job(schedule=schedule) with pytest.raises(exceptions_types.QPUException): assert result == qpu_valid.submit_job(job) # Create a job from a Schedule with a drive with more than one observable # or an observable with coefficient not 1 to check that such Jobs don't work # with the qpu observable = get_observable(TestSimulatedAnnealing.J_valid, TestSimulatedAnnealing.h_valid, TestSimulatedAnnealing.offset_valid) drive_invalid_1 = [(1, observable), (1, observable)] schedule = Schedule(drive=drive_invalid_1) job = schedule.to_job() with pytest.raises(exceptions_types.QPUException): assert result == qpu_valid.submit_job(job) drive_invalid_2 = [(5, observable)] schedule = Schedule(drive=drive_invalid_2) job = schedule.to_job() with pytest.raises(exceptions_types.QPUException): assert result == qpu_valid.submit_job(job) # Solve the problem and check that the returned result is Result result = qpu_valid.submit_job(TestSimulatedAnnealing.job_valid) assert isinstance(result, Result)
def generate_random_circuit(nbqbits: int) -> Circuit: """ Creates a random quantum circuit composed of one or two qubits gates. Args: nbqbits (int): number of qubits in the circuit Returns: Circuit: quantum circuit """ # Build quantum program and quantum register prog = Program() qbits = prog.qalloc(nbqbits) # Build the gate set gate_set = [H, X, Y, Z, CNOT, RX, RY, RZ, PH] # Determine randomly the number of gates applied on the program nb_gates = rd.randint(10 * nbqbits // 2, 10 * nbqbits) # Extract a random list of gates gates = [rd.choice(gate_set) for _ in range(nb_gates)] # Apply gates on prog for gate in gates: if gate in [RX, RY, RZ, PH]: # One qubit parametrised gate # Get a random angle angle = rd.random() * 2 * np.pi # Determine randomly if the gate is controlled is_ctrl = rd.randint(0, 1) if is_ctrl == 0: prog.apply(gate(angle), qbits[rd.randint(0, nbqbits - 1)]) elif is_ctrl == 1: qbit_1, qbit_2 = rd.sample(list(range(nbqbits)), 2) prog.apply(gate(angle).ctrl(), [qbits[qbit_1], qbits[qbit_2]]) elif gate in [H, X, Y, Z]: # Two qubits non-parametrised gate # Determine randomly if the gate is controlled is_ctrl = rd.randint(0, 1) if is_ctrl == 0: prog.apply(gate, qbits[rd.randint(0, nbqbits - 1)]) elif is_ctrl == 1: qbit_1, qbit_2 = rd.sample(list(range(nbqbits)), 2) prog.apply(gate.ctrl(), [qbits[qbit_1], qbits[qbit_2]]) else: # Two qubits gate qbit_1, qbit_2 = rd.sample(list(range(nbqbits)), 2) prog.apply(gate, [qbits[qbit_1], qbits[qbit_2]]) return prog.to_circ()
def test1_asyncqiskit_qpu_4states(self): """ In the case of two H gates, 4 states are expected in output. """ nbqubits = 2 prog = Program() qreg = prog.qalloc(nbqubits) creg = prog.calloc(nbqubits) prog.apply(H, qreg[0]) prog.apply(H, qreg[1]) prog.measure(qreg, creg) qlm_circuit = prog.to_circ() qlm_job = qlm_circuit.to_job(nbshots=1024) # no backend is specified qpu = AsyncBackendToQPU() async_job = qpu.submit_job(qlm_job) string = "\nAsyncBackendToQPU test with a Hadamard on each qubit " \ + "(expects four different measured states):" LOGGER.debug(string) LOGGER.debug("ID: %s\t status : %s", async_job.job_id(), async_job.status()) time.sleep(0.01) LOGGER.debug("ID: %s\t status : %s", async_job.job_id(), async_job.status()) time.sleep(0.2) LOGGER.debug("ID: %s\t status : %s", async_job.job_id(), async_job.status()) loop_nb = 0 while async_job.result() is None: time.sleep(5) loop_nb += 1 if loop_nb > 4: return result = async_job.result() for entry in result.raw_data: LOGGER.debug("State: %s\t probability: %s", entry.state, entry.probability) self.assertEqual(4, len(result.raw_data))
def param_circ(betas, gammas): assert (len(betas) == len(gammas)) p = len(betas) prog = Program() qbits = prog.qalloc(graph.V * l) #Initialization to uniform superposition for q in qbits: prog.apply(H, q) for step in range(p): prog.apply(u_C(gammas[step]), qbits) prog.apply(u_B(betas[step]), qbits) return prog.to_circ()
def check_basic(self, vec): SP = AbstractGate("STATE_PREPARATION", [np.ndarray]) prog = Program() reg = prog.qalloc(4) prog.apply(SP(vec), reg) qpu = PyLinalg() res = qpu.submit(prog.to_circ().to_job()) statevec = np.zeros(2**4, np.complex) for sample in res: statevec[sample.state.int] = sample.amplitude assert (np.linalg.norm(vec - statevec) < 1e-16)
def test3_subset_of_qubits(self): """ Checks if measuring a subset of qubits is working """ prog = Program() qbits = prog.qalloc(2) prog.apply(X, qbits[0]) circ = prog.to_circ() qpu = BackendToQPU(Aer.get_backend('qasm_simulator')) res = qpu.submit(circ.to_job(nbshots=1)) self.assertEqual(res[0].state.int, 0b01) res = qpu.submit(circ.to_job(nbshots=1, qubits=[0])) self.assertEqual(res[0].state.int, 0b1) res = qpu.submit(circ.to_job(nbshots=1, qubits=[1])) self.assertEqual(res[0].state.int, 0b0)
def test_normal_launch_mode_with_nbshots(self): # Create a small program prog = Program() qubits = prog.qalloc(2) prog.apply(H, qubits[0]) prog.apply(CNOT, qubits) circ = prog.to_circ() # Simulate job = circ.to_job(nbshots=4, aggregate_data=False) qpu = PyLinalg() result = qpu.submit_job(job) self.assertEqual(len(result.raw_data), 4) self.assertEqual(result.raw_data[0].probability, None) #no prob if not aggregating data
def create_qprogram(quantum_gate): """ Creates a Quantum Program from an input qlm gate or routine Parameters ---------- quantum_gate : QLM gate or QLM routine Returns ---------- q_prog: QLM Program. Quantum Program from input QLM gate or routine """ q_prog = Program() qbits = q_prog.qalloc(quantum_gate.arity) q_prog.apply(quantum_gate, qbits) return q_prog