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 test_custom_usual_topologies(self) -> None: """ Tests Sabre on common circuit for some usual custom topologies for different number of qubits. """ for nbqbit in range(2 * min_nbqbit, max_nbqbit): nbqbit_circuit = rd.randint(nbqbit // 2, nbqbit) circuit = generate_random_circuit(nbqbit_circuit) observable = generate_random_observable(nbqbit_circuit) qpu_1 = PyLinalg() measure_1 = qpu_1.submit( circuit.to_job("OBS", observable=observable, nbshots=5)) for topology in generate_custom_topologies(nbqbit): qpu_2 = Sabre() | (QuameleonPlugin(topology=topology) | PyLinalg()) measure_2 = qpu_2.submit( circuit.to_job("OBS", observable=observable, nbshots=5)) check_measures_equality(measure_1, measure_2) if nbqbit % 2 == 0: topology = Topology.from_nx( nx.grid_2d_graph(nbqbit // 2, nbqbit // 2)) qpu_2 = Sabre() | (QuameleonPlugin(topology=topology) | PyLinalg()) measure_2 = qpu_2.submit( circuit.to_job("OBS", observable=observable, nbshots=5)) check_measures_equality(measure_1, measure_2)
def test_custom_usual_topologies(self) -> None: """ Tests Sabre on common circuit for some usual custom topologies for different number of qubits. """ for nbqbit in range(2 * min_nbqbit, max_nbqbit): nbqbit_circuit = rd.randint(nbqbit // 2, nbqbit) circuit = generate_random_circuit(nbqbit_circuit) nb_measured_qubits = rd.randint(1, nbqbit_circuit) measured_qubits = rd.sample(range(nbqbit_circuit), nb_measured_qubits) measured_qubits.sort() qpu_1 = PyLinalg() result_1 = qpu_1.submit(circuit.to_job(qubits=measured_qubits)) for topology in generate_custom_topologies(nbqbit): qpu_2 = Sabre() | (QuameleonPlugin(topology=topology) | PyLinalg()) result_2 = qpu_2.submit(circuit.to_job(qubits=measured_qubits)) check_results_equality(result_1, result_2, amplitude=False) if nbqbit % 2 == 0: topology = Topology.from_nx( nx.grid_2d_graph(nbqbit // 2, nbqbit // 2)) qpu_2 = Sabre() | (QuameleonPlugin(topology=topology) | PyLinalg()) result_2 = qpu_2.submit(circuit.to_job(qubits=measured_qubits)) check_results_equality(result_1, result_2, amplitude=False)
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 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_lnn_topology(self) -> None: """ Tests Sabre on common circuit for LNN topologies for different number of qubits. """ for nbqbit in range(2 * min_nbqbit, max_nbqbit): circuit = generate_random_circuit(rd.randint(nbqbit // 2, nbqbit)) qpu_1 = PyLinalg() result_1 = qpu_1.submit(circuit.to_job()) qpu_2 = Sabre() | (QuameleonPlugin(topology=Topology( type=TopologyType.LNN)) | PyLinalg()) result_2 = qpu_2.submit(circuit.to_job()) check_results_equality(result_1, result_2)
def test_qft_lnn(self) -> None: """ Tests Sabre on a QFT for LNN topologies for different number of qubits. """ for nbqbit in range(3, max_nbqbit): circuit = generate_qft_circuit(nbqbit) qpu_1 = PyLinalg() result_1 = qpu_1.submit(circuit.to_job()) qpu_2 = Sabre() | (QuameleonPlugin(topology=Topology( type=TopologyType.LNN)) | PyLinalg()) result_2 = qpu_2.submit(circuit.to_job()) check_results_equality(result_1, result_2)
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 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 test_qft_custom_usual_topologies(self) -> None: """ Tests Sabre on a QFT for some usual custom topologies for different number of qubits. """ for nbqbit in range(5, max_nbqbit): circuit = generate_qft_circuit(nbqbit) qpu_1 = PyLinalg() result_1 = qpu_1.submit(circuit.to_job()) for topology in generate_custom_topologies(nbqbit): print('Number of qubits : ', nbqbit) print('Current topology : ', topology) qpu_2 = Sabre() | (QuameleonPlugin(topology=topology) | PyLinalg()) result_2 = qpu_2.submit(circuit.to_job()) check_results_equality(result_1, result_2)
def test_measure(self): """test that state indexing is same as other simulation services""" # program with final state: qbit 0 : 0 or 1 with 50% proba prog = Program() reg = prog.qalloc(1) creg = prog.calloc(1) prog.apply(H, reg) prog.measure(reg, creg) circ = prog.to_circ() qpu = PyLinalg() result = qpu.submit(circ.to_job(nbshots=5, aggregate_data=False)) for res in result: self.assertAlmostEqual( res.intermediate_measurements[0].probability, 0.5, delta=1e-10) self.assertEqual(res.intermediate_measurements[0].cbits[0], res.state.int)
def test_lnn_topology(self) -> None: """ Tests Sabre on common circuit for LNN topologies for different number of qubits. """ for nbqbit in range(2 * min_nbqbit, max_nbqbit): nbqbit_circuit = rd.randint(nbqbit // 2, nbqbit) circuit = generate_random_circuit(nbqbit_circuit) observable = generate_random_observable(nbqbit_circuit) qpu_1 = PyLinalg() measure_1 = qpu_1.submit( circuit.to_job("OBS", observable=observable)) qpu_2 = Sabre() | (QuameleonPlugin(topology=Topology( type=TopologyType.LNN)) | PyLinalg()) measure_2 = qpu_2.submit( circuit.to_job("OBS", observable=observable)) check_measures_equality(measure_1, measure_2)
def test_lnn_topology(self) -> None: """ Tests Sabre on common circuit for LNN topologies for different number of qubits. """ for nbqbit in range(2 * min_nbqbit, max_nbqbit): nbqbit_circuit = rd.randint(nbqbit // 2, nbqbit) circuit = generate_random_circuit(nbqbit_circuit) nb_measured_qubits = rd.randint(1, nbqbit_circuit) measured_qubits = rd.sample(range(nbqbit_circuit), nb_measured_qubits) measured_qubits.sort() qpu_1 = PyLinalg() result_1 = qpu_1.submit(circuit.to_job(qubits=measured_qubits)) qpu_2 = Sabre() | (QuameleonPlugin(topology=Topology( type=TopologyType.LNN)) | PyLinalg()) result_2 = qpu_2.submit(circuit.to_job(qubits=measured_qubits)) check_results_equality(result_1, result_2, amplitude=False)