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 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 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)