def test_middle_engine() -> None: # pylint: disable=pointless-statement # pylint: disable=expression-not-assigned opti = tketOptimiser() engines = [opti] eng = MainEngine(engine_list=engines) circ = Circuit(2) circ.H(0) circ.CX(0, 1) circ.CX(0, 1) circ.CX(0, 1) circ.Rx(0.2, 1) circ.Rx(-0.2, 1) qureg = eng.allocate_qureg(circ.n_qubits) tk_to_projectq(eng, qureg, circ) Rz(0.3) | qureg[0] Rz(-0.3) | qureg[0] H | qureg[1] H | qureg[1] X | qureg[1] eng.flush() p1 = eng.backend.get_probability([0, 0], qureg) p2 = eng.backend.get_probability([0, 1], qureg) p3 = eng.backend.get_probability([1, 0], qureg) p4 = eng.backend.get_probability([1, 1], qureg) assert isclose(p1, 0.0, abs_tol=eps) assert isclose(p2, 0.5, abs_tol=eps) assert isclose(p3, 0.5, abs_tol=eps) assert isclose(p4, 0.0, abs_tol=eps)
def h2_4q_circ(theta: float) -> Circuit: circ = Circuit(4).X(0).X(1) circ.Rx(0.5, 0).H(1).H(2).H(3) circ.CX(0, 1).CX(1, 2).CX(2, 3) circ.Rz((-2 / np.pi) * theta, 3) circ.CX(2, 3).CX(1, 2).CX(0, 1) circ.Rx(-0.5, 0).H(1).H(2).H(3) return circ
def h2_2q_circ(theta: float) -> Circuit: circ = Circuit(2).X(0) circ.Rx(0.5, 0).H(1) circ.CX(0, 1) circ.Rz((-2 / np.pi) * theta, 1) circ.CX(0, 1) circ.Rx(-0.5, 0).H(1) return circ
def test_delay_measures() -> None: b = ForestBackend("9q-square") # No triangles in architecture, so third CX will need a bridge # This will happen after the measurement on qubit 1 c = Circuit(3, 3) c.CX(0, 1) c.CX(1, 2) c.CX(0, 2) c.Measure(0, 0) c.Measure(1, 1) c.Measure(2, 2) b.compile_circuit(c) assert b.valid_circuit(c)
def test_honeywell() -> None: token = os.getenv("HQS_AUTH") backend = HoneywellBackend( device_name="HQS-LT-1.0-APIVAL", machine_debug=skip_remote_tests ) c = Circuit(4, 4, "test 1") c.H(0) c.CX(0, 1) c.Rz(0.3, 2) c.CSWAP(0, 1, 2) c.CRz(0.4, 2, 3) c.CY(1, 3) c.ZZPhase(0.1, 2, 0) c.Tdg(3) c.measure_all() backend.compile_circuit(c) n_shots = 4 handle = backend.process_circuits([c], n_shots)[0] correct_shots = np.zeros((4, 4)) correct_counts = {(0, 0, 0, 0): 4} res = backend.get_result(handle, timeout=49) shots = res.get_shots() counts = res.get_counts() assert backend.circuit_status(handle).status is StatusEnum.COMPLETED assert np.all(shots == correct_shots) assert counts == correct_counts newshots = backend.get_shots(c, 4, timeout=49) assert np.all(newshots == correct_shots) newcounts = backend.get_counts(c, 4) assert newcounts == correct_counts if token is None: assert backend.device is None
def test_incrementer() -> None: """ Simulate an 8-bit incrementer """ b = QsharpToffoliSimulatorBackend() c = Circuit(8) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4, 5, 6, 7]) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4, 5, 6]) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4, 5]) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4]) c.add_gate(OpType.CnX, [0, 1, 2, 3]) c.CCX(0, 1, 2) c.CX(0, 1) c.X(0) for x in [0, 23, 79, 198, 255]: # some arbitrary 8-bit numbers circ = Circuit(8) # prepare the state corresponding to x for i in range(8): if (x >> i) % 2 == 1: circ.X(i) # append the incrementer circ.add_circuit(c, list(range(8))) circ.measure_all() # run the simulator b.compile_circuit(circ) bits = b.get_shots(circ, 1)[0] # check the result for i in range(8): assert bits[i] == ((x + 1) >> i) % 2
def test_estimates() -> None: """ Check that the resource estimator gives reasonable results. """ b = QsharpEstimatorBackend() c = Circuit(3) c.H(0) c.CX(0, 1) c.CCX(0, 1, 2) c.Rx(0.3, 1) c.Ry(0.4, 2) c.Rz(1.1, 0) c.S(1) c.SWAP(0, 2) c.T(1) c.X(0) c.Y(1) c.Z(2) pbox = PauliExpBox([Pauli.X, Pauli.I, Pauli.Z], 0.25) c.add_pauliexpbox(pbox, [2, 0, 1]) b.compile_circuit(c, 0) resources = b.get_resources(c) assert resources["CNOT"] >= 1 assert resources["QubitClifford"] >= 1 assert resources["R"] >= 1 assert resources["T"] >= 1 assert resources["Depth"] >= 1 assert resources["Width"] == 3 assert resources["BorrowedWidth"] == 0
def test_ibmq_emulator() -> None: b_emu = IBMQEmulatorBackend("ibmq_santiago", hub="ibm-q", group="open", project="main") assert b_emu._noise_model is not None b_ibm = b_emu._ibmq b_aer = AerBackend() for ol in range(3): comp_pass = b_emu.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c_cop = c.copy() comp_pass.apply(c_cop) c.measure_all() for bac in (b_emu, b_ibm): assert all(pred.verify(c_cop) for pred in bac.required_predicates) c_cop_2 = c.copy() b_aer.compile_circuit(c_cop_2, ol) if ol == 0: assert not all( pred.verify(c_cop_2) for pred in b_emu.required_predicates) circ = Circuit(2, 2).H(0).CX(0, 1).measure_all() b_emu.compile_circuit(circ) b_noi = AerBackend(noise_model=b_emu._noise_model) emu_shots = b_emu.get_shots(circ, 10, seed=10) aer_shots = b_noi.get_shots(circ, 10, seed=10) assert np.array_equal(emu_shots, aer_shots)
def test_aer_placed_expectation() -> None: # bug TKET-695 n_qbs = 3 c = Circuit(n_qbs, n_qbs) c.X(0) c.CX(0, 2) c.CX(1, 2) c.H(1) # c.measure_all() b = AerBackend() operator = QubitPauliOperator({ QubitPauliString(Qubit(0), Pauli.Z): 1.0, QubitPauliString(Qubit(1), Pauli.X): 0.5, }) assert b.get_operator_expectation_value(c, operator) == (-0.5 + 0j) with open(os.path.join(sys.path[0], "ibmqx2_properties.pickle"), "rb") as f: properties = pickle.load(f) noise_model = NoiseModel.from_backend(properties) noise_b = AerBackend(noise_model) with pytest.raises(RuntimeError) as errorinfo: noise_b.get_operator_expectation_value(c, operator) assert "not supported with noise model" in str(errorinfo.value) c.rename_units({Qubit(1): Qubit("node", 1)}) with pytest.raises(ValueError) as errorinfoCirc: b.get_operator_expectation_value(c, operator) assert "default register Qubits" in str(errorinfoCirc.value)
def circuit_gen(measure: bool = False) -> Circuit: c = Circuit(2, 2) c.H(0) c.CX(0, 1) if measure: c.measure_all() return c
def circuit_gen(graph): """Generate the initial, unrouted circuit with the CXs, given a networkx graph Parameters ---------- mol_graph : networkx graph Networkx graph with nodes:atoms and edges:bonds Returns ------- pytket Circuit simple pytket circuit with a qubit for each atom Notes ----- The qubits are labelled with integers that match the integers found in the inputted networkx graph node labels""" # - generate list of edges along with list of inverted edges # - combine the inverted edges with non-inverted ones edges = list(graph.edges) inv_edges = [(edge[1], edge[0]) for edge in edges] cxs = [val for pair in zip(edges, inv_edges) for val in pair] # Create circuit and unpack each edge, applying it as a CX constraint_test_circuit = Circuit(graph.number_of_nodes()) for edge in cxs: constraint_test_circuit.CX(*edge) # return the mapping from atoms to integers and the generated constraint circuit return constraint_test_circuit
def test_convert() -> None: c = Circuit(3) c.H(0) c.H(1) c.CX(1, 0) c.X(1) pbox = PauliExpBox([Pauli.X, Pauli.Z, Pauli.X], 0.25) c.add_pauliexpbox(pbox, [2, 0, 1]) qs = tk_to_qsharp(c) assert "H(q[1]);" in qs
def random_sparse_ansatz(n_qubits, n_layers, p, rng_seed=None): seed(rng_seed) circ = Circuit(n_qubits) for q in range(n_qubits): if random() < p: circ.Ry(0.1 * randrange(20), q) for l in range(n_layers): for q in range(0, n_qubits - 1, 2): circ.CX(q, q + 1) for q in range(2 * (n_qubits // 2)): if random() < p: circ.Ry(0.1 * randrange(20), q) for q in range(1, n_qubits - 1, 2): circ.CX(q, q + 1) for q in range(2 * ((n_qubits - 1) // 2)): if random() < p: circ.Ry(0.1 * randrange(20), q + 1) circ.measure_all() return circ
def test_swaps_basisorder() -> None: # Check that implicit swaps can be corrected irrespective of BasisOrder b = AerStateBackend() c = Circuit(4) c.X(0) c.CX(0, 1) c.CX(1, 0) c.CX(1, 3) c.CX(3, 1) c.X(2) cu = CompilationUnit(c) CliffordSimp(True).apply(cu) c1 = cu.circuit assert c1.n_gates_of_type(OpType.CX) == 2 b.compile_circuit(c) b.compile_circuit(c1) handles = b.process_circuits([c, c1]) s_ilo = b.get_state(c1, basis=BasisOrder.ilo) correct_ilo = b.get_state(c, basis=BasisOrder.ilo) assert np.allclose(s_ilo, correct_ilo) s_dlo = b.get_state(c1, basis=BasisOrder.dlo) correct_dlo = b.get_state(c, basis=BasisOrder.dlo) assert np.allclose(s_dlo, correct_dlo) qbs = c.qubits for result in b.get_results(handles): assert (result.get_state([qbs[1], qbs[2], qbs[3], qbs[0]]).real.tolist().index(1.0) == 6) assert (result.get_state([qbs[2], qbs[1], qbs[0], qbs[3]]).real.tolist().index(1.0) == 9) assert (result.get_state([qbs[2], qbs[3], qbs[0], qbs[1]]).real.tolist().index(1.0) == 12) bu = AerUnitaryBackend() u_ilo = bu.get_unitary(c1, basis=BasisOrder.ilo) correct_ilo = bu.get_unitary(c, basis=BasisOrder.ilo) assert np.allclose(u_ilo, correct_ilo) u_dlo = bu.get_unitary(c1, basis=BasisOrder.dlo) correct_dlo = bu.get_unitary(c, basis=BasisOrder.dlo) assert np.allclose(u_dlo, correct_dlo)
def test_machine_debug() -> None: b = IonQBackend(api_key="invalid", device_name="simulator", label="test 5") b._MACHINE_DEBUG = True c = Circuit(2) c.H(0) c.CX(0, 1) c.measure_all() n_shots = 100 counts = b.get_counts(c, n_shots=n_shots, timeout=30) assert counts[(0, 0)] == n_shots
def test_machine_debug() -> None: b = AQTBackend("invalid", device_name="sim", label="test 6") b._MACHINE_DEBUG = True c = Circuit(2, 2) c.H(0) c.CX(0, 1) c.measure_all() b.compile_circuit(c) n_shots = 10 counts = b.get_counts(c, n_shots=n_shots, timeout=30) assert counts == {(0, 0): n_shots}
def test_bell() -> None: b = HoneywellBackend(device_name="HQS-LT-1.0-APIVAL") c = Circuit(2, 2, "test 2") c.H(0) c.CX(0, 1) c.measure_all() b.compile_circuit(c) n_shots = 10 shots = b.get_shots(c, n_shots) print(shots) assert all(q[0] == q[1] for q in shots)
def test_default_pass() -> None: b = ProjectQBackend() for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_rebase_CX() -> None: circ = Circuit(2) circ.CX(0, 1) orig_circ = circ.copy() _aqt_rebase().apply(circ) # TODO use tketsim for this test once available u1 = AerUnitaryBackend().get_unitary(orig_circ) u2 = AerUnitaryBackend().get_unitary(circ) assert np.allclose(u1, u2)
def test_bell() -> None: # On the noiseless simulator, we should always get Bell states here. token = cast(str, os.getenv("AQT_AUTH")) b = AQTBackend(token, device_name="sim", label="test 2") c = Circuit(2, 2) c.H(0) c.CX(0, 1) c.measure_all() b.compile_circuit(c) n_shots = 10 counts = b.get_counts(c, n_shots, timeout=30) assert all(q[0] == q[1] for q in counts)
def test_default_pass() -> None: b = AQTBackend("invalid", device_name="sim/noise-model-1") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_big_circuit_ionq() -> None: token = cast(str, os.getenv("IONQ_AUTH")) backend = IonQBackend(api_key=token, device_name="simulator", label="test 2") circ = Circuit(4) circ.X(0).Y(0).Z(0).H(1).S(1).Sdg(1).H(1).T(2).Tdg(2).V(3).Vdg(3) circ.SWAP(0, 1) circ.CX(3, 2) circ.ZZPhase(1.2, 0, 1) circ.measure_all() counts = backend.get_counts(circ, n_shots=100) assert counts[(0, 0, 0, 0)] == 100
def test_cancellation() -> None: token = cast(str, os.getenv("IONQ_AUTH")) b = IonQBackend(api_key=token, device_name="simulator", label="test 6") qc = Circuit(3, 3) qc.H(0) qc.CX(0, 2) qc.Measure(0, 1) qc.Measure(1, 0) qc.Measure(2, 2) b.compile_circuit(qc) h = b.process_circuit(qc, n_shots=100) b.cancel(h)
def test_default_pass() -> None: b = HoneywellBackend(device_name="HQS-LT-1.0-APIVAL") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_handles() -> None: b = QsharpToffoliSimulatorBackend() c = Circuit(4) c.CX(0, 1) c.CCX(0, 1, 2) c.add_gate(OpType.CnX, [0, 1, 2, 3]) c.add_gate(OpType.noop, [2]) c.X(3) c.SWAP(1, 2) c.measure_all() b.compile_circuit(c) shots = b.get_shots(c, n_shots=2) assert all(shots[0] == shots[1])
def test_from_tket() -> None: c = Circuit(4, 2) c.X(0) c.H(1) c.S(1) c.CX(2, 0) c.Ry(0.5, 3) c.Measure(3, 0) c.Measure(1, 1) p = tk_to_pyquil(c) assert ( len(p.instructions) == 8 ) # 5 gates, 2 measures, and an initial declaration of classical register
def test_default_pass() -> None: b = ForestBackend("9q-square") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_default_pass() -> None: b = IBMQBackend("ibmq_santiago", hub="ibm-q", group="open", project="main") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_ibmq_mid_measure() -> None: c = Circuit(3, 3).H(1).CX(1, 2).Measure(0, 0).Measure(1, 1) c.add_barrier([0, 1, 2]) c.CX(1, 0).H(0).Measure(2, 2) b = IBMQEmulatorBackend("ibmq_athens", hub="ibm-q", group="open", project="main") b.compile_circuit(c) assert not NoMidMeasurePredicate().verify(c) assert b.valid_circuit(c)
def test_default_pass() -> None: b = QsharpSimulatorBackend() for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(4, 4) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)