Пример #1
0
def test_classical() -> None:
    # circuit to cover capabilities covered in HQS example notebook
    c = Circuit(1)
    a = c.add_c_register("a", 8)
    b = c.add_c_register("b", 10)
    c.add_c_register("c", 10)

    c.add_c_setbits([True], [a[0]])
    c.add_c_setbits([False, True] + [False] * 6, list(a))
    c.add_c_setbits([True, True] + [False] * 8, list(b))

    c.X(0, condition=reg_eq(a ^ b, 1))
    c.X(0, condition=(a[0] ^ b[0]))
    c.X(0, condition=reg_eq(a & b, 1))
    c.X(0, condition=reg_eq(a | b, 1))

    c.X(0, condition=a[0])
    c.X(0, condition=reg_neq(a, 1))
    c.X(0, condition=if_not_bit(a[0]))
    c.X(0, condition=reg_gt(a, 1))
    c.X(0, condition=reg_lt(a, 1))
    c.X(0, condition=reg_geq(a, 1))
    c.X(0, condition=reg_leq(a, 1))

    b = HoneywellBackend("HQS-LT-S1-APIVAL")

    b.compile_circuit(c)
    assert b.get_counts(c, 10)
Пример #2
0
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
Пример #3
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)
Пример #4
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)
Пример #5
0
def test_multireg() -> None:
    b = HoneywellBackend(device_name="HQS-LT-1.0-APIVAL", label="test 3")
    c = Circuit()
    q1 = Qubit("q1", 0)
    q2 = Qubit("q2", 0)
    c1 = Bit("c1", 0)
    c2 = Bit("c2", 0)
    for q in (q1, q2):
        c.add_qubit(q)
    for cb in (c1, c2):
        c.add_bit(cb)
    c.H(q1)
    c.CX(q1, q2)
    c.Measure(q1, c1)
    c.Measure(q2, c2)
    b.compile_circuit(c)

    n_shots = 10
    shots = b.get_shots(c, n_shots)
    assert np.array_equal(shots, np.zeros((10, 2)))
Пример #6
0
def test_cost_estimate(c: Circuit, n_shots: int) -> None:

    b = HoneywellBackend("HQS-LT-S1-APIVAL")
    b.compile_circuit(c)
    estimate = b.cost_estimate(c, n_shots)
    status = b.circuit_status(b.process_circuit(c, n_shots))
    status_msg = status.message
    message_dict = literal_eval(status_msg)
    if message_dict["cost"] is not None:
        api_cost = float(message_dict["cost"])
        assert estimate == api_cost
Пример #7
0
def test_cancel() -> None:
    b = HoneywellBackend(device_name="HQS-LT-1.0-APIVAL", label="test cancel")
    c = Circuit(2, 2).H(0).CX(0, 1).measure_all()
    b.compile_circuit(c)
    handle = b.process_circuit(c, 10)
    try:
        # will raise HTTP error if job is already completed
        b.cancel(handle)
    except HQSAPIError as err:
        check_completed = "job has completed already" in str(err)
        assert check_completed
        if not check_completed:
            raise err

    print(b.circuit_status(handle))
Пример #8
0
def test_shots_bits_edgecases(n_shots, n_bits) -> None:

    honeywell_backend = HoneywellBackend("HQS-LT-1.0-APIVAL", machine_debug=True)
    c = Circuit(n_bits, n_bits)

    # TODO TKET-813 add more shot based backends and move to integration tests
    h = honeywell_backend.process_circuit(c, n_shots)
    res = honeywell_backend.get_result(h)

    correct_shots = np.zeros((n_shots, n_bits), dtype=int)
    correct_shape = (n_shots, n_bits)
    correct_counts = Counter({(0,) * n_bits: n_shots})
    # BackendResult
    assert np.array_equal(res.get_shots(), correct_shots)
    assert res.get_shots().shape == correct_shape
    assert res.get_counts() == correct_counts

    # Direct
    assert np.array_equal(honeywell_backend.get_shots(c, n_shots), correct_shots)
    assert honeywell_backend.get_shots(c, n_shots).shape == correct_shape
    assert honeywell_backend.get_counts(c, n_shots) == correct_counts
Пример #9
0
)
tp = TketPass(seq)
pm = PassManager(
    [
        # Insert initial qiskit passes
        Unroller(["cx", "p", "u"]),
        tp,
        # Insert final qiskit passes
    ]
)
qi = QuantumInstance(backend, pass_manager=pm)

# Similarly, when using `TketBackend` it may be necessary to include some compilation in `qiskit` to enable the conversion into `pytket`, and then some further `pytket` compilation to get it suitable for the actual target backend. For example, `qiskit.circuit.library.standard_gates.DCXGate` currently does not have an equivalent elementary operation in `pytket`, so must be decomposed before we can map across, and likewise the `OpType.ZZMax` gate used by `pytket.extensions.honeywell.HoneywellBackend` (from the `pytket-honeywell` extension) has no equivalent in `qiskit` so the targeting of the final gateset must be performed by `pytket`.

from pytket.extensions.honeywell import HoneywellBackend

pm = PassManager(
    Unroller(["cx", "p", "u"])
)  # Map to a basic gateset to allow conversion to pytket

hb = HoneywellBackend(device_name="HQS-LT-1.0-APIVAL", machine_debug=True)
backend = TketBackend(
    hb, hb.default_compilation_pass(2)
)  # Then use pytket compilation with optimisation level 2

qi = QuantumInstance(backend, pass_manager=pm)

# Exercises:
# - Try running the GAS example using another device/simulator through the `TketBackend` wrapper. The simplest option is to just go full-circle and use the `AerBackend` from `pytket-qiskit` which itself is a wrapper around the `qasm_simulator`.
# - Take another example notebook from the Qiskit [official tutorials](https://github.com/Qiskit/qiskit-tutorials) or [community tutorials](https://github.com/qiskit-community/qiskit-community-tutorials) and try changing out the backend used to a `pytket` backend. For example, try running the [Qiskit Chemistry example](https://github.com/Qiskit/qiskit-tutorials/blob/master/tutorials/chemistry/1_programmatic_approach.ipynb) using a Rigetti device (or simulator) with `ForestBackend` from `pytket-pyquil`, and add in `pytket.passes.PauliSimp` to exploit the structure of the UCCSD ansatz.