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 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_algorithm(circuit, iterations=(1000000)): """ Tests a circuit by submitting it to both aer_simulator and PyLinalg. """ linalg = PyLinalg() qlm_circ, _ = qiskit_to_qlm(circuit, sep_measures=True) test_job = qlm_circ.to_job(nbshots=0, aggregate_data=False) expected = linalg.submit(test_job) qiskit_qpu = BackendToQPU(Aer.get_backend('aer_simulator')) test_job.nbshots = iterations result = qiskit_qpu.submit(test_job) dist_calc = compare_results(expected, result, aggregate=False) distance = analyze_distance(dist_calc) print("Distance is {}".format(distance)) return distance
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 test0_qiskit_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 = BackendToQPU(backend) results = qpu.submit(batch) string = "\nBackendToQPU test with a Hadamard and a CNOT " \ + "(expects two different measured states):" LOGGER.debug(string) for result in results.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 test0_default_gates_and_qbit_reorder(self): """ Tries out every default gate and check that they match once the Qiskit circuit is translated into a QLM circuit. """ qreg1 = QuantumRegister(2) qreg2 = QuantumRegister(1) qreg3 = QuantumRegister(2) creg = ClassicalRegister(5) ocirc = QuantumCircuit(qreg1, qreg2, qreg3, creg) gates_1qb_0prm, gates_1qb_1prm, gates_2qb_0prm, \ gates_2qb_1prm, gates_3qb_0prm = gen_gates(ocirc) for gate_op in gates_1qb_0prm: gate_op(qreg2[0]) for gate_op in gates_1qb_1prm: gate_op(3.14, qreg2[0]) for gate_op in gates_2qb_0prm: gate_op(qreg3[0], qreg1[1]) for gate_op in gates_2qb_1prm: gate_op(3.14, qreg3[0], qreg1[1]) for gate_op in gates_3qb_0prm: gate_op(qreg2[0], qreg3[1], qreg1[1]) ocirc.u(3.14, 3.14, 3.14, qreg3[0]) ocirc.r(3.14, 3.14, qreg3[0]) ocirc.ms(3.14, [qreg1[1], qreg2[0], qreg3[0]]) ocirc.measure(qreg1[0], creg[4]) ocirc.measure(qreg1[1], creg[3]) ocirc.measure(qreg2[0], creg[2]) ocirc.measure(qreg3[0], creg[1]) ocirc.measure(qreg3[1], creg[0]) result = qiskit_to_qlm(ocirc) prog = Program() qubits = prog.qalloc(5) cbits = prog.calloc(5) for gate_op in PYGATES_1QB: prog.apply(gate_op, qubits[2]) for gate_op in PYGATES_2QB: prog.apply(gate_op, qubits[3], qubits[1]) prog.apply(SWAP.ctrl(), qubits[2], qubits[4], qubits[1]) prog.apply(X.ctrl().ctrl(), qubits[2], qubits[4], qubits[1]) prog.apply(U3(3.14, 3.14, 3.14), qubits[3]) prog.apply(R(3.14, 3.14), qubits[3]) prog.apply(MS(3.14, 3), qubits[1], qubits[2], qubits[3]) for i in range(5): prog.measure(qubits[i], cbits[4 - i]) expected = prog.to_circ() self.assertEqual(len(result.ops), len(expected.ops)) for res_op, exp_op in zip(result.ops, expected.ops): 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) LOGGER.debug("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) LOGGER.debug("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) LOGGER.debug("\nResults obtained:") qpu = BackendToQPU() result_job = result.to_job(nbshots=1024) qiskit_result = qpu.submit(result_job) for entry in qiskit_result.raw_data: LOGGER.debug("State: {}\t probability: {}".format( entry.state, entry.probability)) LOGGER.debug("\nResults expected:") expected_job = expected.to_job(nbshots=1024) qlm_result = qpu.submit(expected_job) for entry in qlm_result.raw_data: LOGGER.debug("State: {}\t probability: {}".format( entry.state, entry.probability)) self.assertEqual(len(qiskit_result.raw_data), len(qlm_result.raw_data)) states_expected = [str(entry.state) for entry in qlm_result.raw_data] for entry in qiskit_result.raw_data: self.assertTrue(str(entry.state) in states_expected)