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 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 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 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
Esempio n. 5
0
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
Esempio n. 6
0
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 test_no_measure_shots() -> None:
    for b in backends:
        c = Circuit(2, 2)
        c.H(0).CX(0, 1)
        # Note, no measurements
        b.compile_circuit(c)
        handle = b.process_circuit(c, n_shots=10)
        counts = b.get_result(handle).get_counts()
        assert counts == {(0, 0): 10}
Esempio n. 8
0
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
Esempio n. 9
0
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
Esempio n. 10
0
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)
Esempio n. 11
0
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}
Esempio n. 12
0
def test_routing_no_cx() -> None:
    circ = Circuit(2, 2)
    circ.H(1)
    # c.CX(1, 2)
    circ.Rx(0.2, 0)
    circ.measure_all()
    coupling = [[1, 0], [2, 0], [2, 1], [3, 2], [3, 4], [4, 2]]
    arc = Architecture(coupling)
    physical_c = route(circ, arc)

    assert len(physical_c.get_commands()) == 4
def test_statevector_phase() -> None:
    for b in backends:
        circ = Circuit(2)
        circ.H(0).CX(0, 1)
        b.compile_circuit(circ)
        state = b.get_state(circ)
        assert np.allclose(
            state, [math.sqrt(0.5), 0, 0, math.sqrt(0.5)], atol=1e-10)
        circ.add_phase(0.5)
        state1 = b.get_state(circ)
        assert np.allclose(state1, state * 1j, atol=1e-10)
Esempio n. 14
0
def test_H() -> None:
    circ = Circuit(1)
    circ.H(0)
    eng = MainEngine()
    qureg = eng.allocate_qureg(circ.n_qubits)
    tk_to_projectq(eng, qureg, circ)
    eng.flush()
    p1 = eng.backend.get_probability([0], qureg)
    p2 = eng.backend.get_probability([1], qureg)
    assert isclose(p1, 0.5, abs_tol=eps)
    assert isclose(p2, 0.5, abs_tol=eps)
    All(Measure) | qureg  # pylint: disable=expression-not-assigned
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)
Esempio n. 16
0
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)
Esempio n. 17
0
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)
Esempio n. 18
0
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
Esempio n. 19
0
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)
Esempio n. 20
0
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)
Esempio n. 21
0
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)
Esempio n. 22
0
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)
Esempio n. 23
0
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_statevector() -> None:
    b = AerStateBackend()
    circ = Circuit(3, name="test")
    circ.H(2)
    circ.X(0)
    circ.H(0)
    circ.CX(0, 1)
    circ.CZ(1, 2)
    circ.Sdg(0)
    circ.Tdg(1)
    circ.Z(1)
    circ.T(2)
    circ.Rx(0.3333, 1)
    circ.Rz(0.3333, 1)
    zxcirc = tk_to_pyzx(circ)
    assert zxcirc.name == circ.name
    b.compile_circuit(circ)
    state = b.get_state(circ)
    circ2 = pyzx_to_tk(zxcirc)
    assert circ2.name == circ.name
    b.compile_circuit(circ2)
    state2 = b.get_state(circ2)
    assert np.allclose(state, state2, atol=1e-10)
Esempio n. 25
0
def test_mixed_circuit() -> None:
    c = Circuit()
    qr = c.add_q_register("q", 2)
    ar = c.add_c_register("a", 1)
    br = c.add_c_register("b", 1)
    c.H(qr[0])
    c.Measure(qr[0], ar[0])
    c.X(qr[1], condition=reg_eq(ar, 0))
    c.Measure(qr[1], br[0])
    backend = AerBackend()
    backend.compile_circuit(c)
    counts = backend.get_counts(c, 1024)
    for key in counts.keys():
        assert key in {(0, 1), (1, 0)}
Esempio n. 26
0
def test_compilation_correctness() -> None:
    c = Circuit(5)
    c.H(0).H(1).H(2)
    c.CX(0, 1).CX(1, 2)
    c.Rx(0.25, 1).Ry(0.75, 1).Rz(0.5, 2)
    c.CCX(2, 1, 0)
    c.CY(1, 0).CY(2, 1)
    c.H(0).H(1).H(2)
    c.Rz(0.125, 0)
    c.X(1)
    c.Rz(0.125, 2).X(2).Rz(0.25, 2)
    c.SX(3).Rz(0.125, 3).SX(3)
    c.CX(0, 3).CX(0, 4)
    u_backend = AerUnitaryBackend()
    u = u_backend.get_unitary(c)
    ibm_backend = IBMQBackend("ibmq_santiago",
                              hub="ibm-q",
                              group="open",
                              project="main")
    for ol in range(3):
        p = ibm_backend.default_compilation_pass(optimisation_level=ol)
        cu = CompilationUnit(c)
        p.apply(cu)
        c1 = cu.circuit
        compiled_u = u_backend.get_unitary(c1)

        # Adjust for placement
        imap = cu.initial_map
        fmap = cu.final_map
        c_idx = {c.qubits[i]: i for i in range(5)}
        c1_idx = {c1.qubits[i]: i for i in range(5)}
        ini = {c_idx[qb]: c1_idx[node] for qb, node in imap.items()}
        inv_fin = {c1_idx[node]: c_idx[qb] for qb, node in fmap.items()}
        m_ini = lift_perm(ini)
        m_inv_fin = lift_perm(inv_fin)

        assert compare_unitaries(u, m_inv_fin @ compiled_u @ m_ini)
Esempio n. 27
0
def test_convert() -> None:
    circ = Circuit(4, 4)
    circ.H(0).CX(0, 1)
    circ.add_gate(OpType.noop, [1])
    circ.CRz(0.5, 1, 2)
    circ.add_barrier([2])
    circ.ZZPhase(0.3, 2, 3).CX(3, 0).Tdg(1)
    circ.Measure(0, 0)
    circ.Measure(1, 2)
    circ.Measure(2, 3)
    circ.Measure(3, 1)

    circ_aqt = tk_to_aqt(circ)
    assert json.loads(circ_aqt[1]) == [0, 3, 1, 2]
    assert all(gate[0] in ["X", "Y", "MS"] for gate in circ_aqt[0])
Esempio n. 28
0
def test_bell() -> None:
    """
    Simulate a circuit that generates a Bell state, and check that the results
    are all (0,0) or (1,1).
    """
    b = QsharpSimulatorBackend()
    c = Circuit(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)
    assert all(m[0] == m[1] for m in counts.keys())
    assert sum(counts.values()) == n_shots
Esempio n. 29
0
def measurement_circuits(hamiltonian, n_qubits):
    all_circs = []
    for pauli, _ in hamiltonian.terms.items():
        if not pauli:
            continue  # Ignore the constant term
        measure_circ = Circuit(n_qubits, n_qubits)
        for qb, p in pauli:
            if p == "I":
                continue  # Discard I qubits
            elif p == "X":
                measure_circ.H(qb)
            elif p == "Y":
                measure_circ.Rx(0.5, qb)
            measure_circ.Measure(qb, qb)
        all_circs.append(measure_circ)
    return all_circs
Esempio n. 30
0
def test_handles() -> None:
    token = cast(str, os.getenv("AQT_AUTH"))
    b = AQTBackend(token, device_name="sim/noise-model-1", label="test 5")
    c = Circuit(2, 2)
    c.H(0)
    c.CX(0, 1)
    c.measure_all()
    b.compile_circuit(c)
    n_shots = 5
    shots = b.get_shots(c, n_shots=n_shots, timeout=30)
    assert len(shots) == n_shots
    counts = b.get_counts(c, n_shots=n_shots)
    assert sum(counts.values()) == n_shots
    handles = b.process_circuits([c, c], n_shots=n_shots)
    assert len(handles) == 2
    for handle in handles:
        assert b.circuit_status(handle).status is StatusEnum.COMPLETED