Example #1
0
def test_shots_bits_edgecases(qvm: None, quilc: None) -> None:
    print(type(qvm))
    print(type(quilc))
    forest_backend = ForestBackend("9q-square")

    for n_bits in range(1, 9):  # Getting runtime error if n_qubit > 9.
        for n_shots in range(1, 11):
            c = Circuit(n_bits, n_bits)

            # TODO TKET-813 add more shot based backends and move to integration tests
            forest_backend.compile_circuit(c)
            h = forest_backend.process_circuit(c, n_shots)
            res = forest_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(forest_backend.get_shots(c, n_shots),
                                  correct_shots)
            assert forest_backend.get_shots(c, n_shots).shape == correct_shape
            assert forest_backend.get_counts(c, n_shots) == correct_counts
Example #2
0
def test_handle() -> None:
    b = ForestBackend("9q-square")
    c0 = Circuit(1)
    c0.measure_all()
    c1 = Circuit(1)
    c1.X(0)
    c1.measure_all()
    b.compile_circuit(c0)
    b.compile_circuit(c1)
    counts0 = b.get_counts(c0, n_shots=10)
    counts1 = b.get_counts(c1, n_shots=10)
    assert counts0 == {(0, ): 10}
    assert counts1 == {(1, ): 10}
Example #3
0
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)
Example #4
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)
Example #5
0
def test_pauli_sim(qvm: None, quilc: None) -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    b = ForestBackend("9q-square")
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    energy = get_pauli_expectation_value(c, zi, b, 10)
    assert abs(energy - 1) < 0.001
    c.X(0)
    energy = get_pauli_expectation_value(c, zi, b, 10)
    assert abs(energy + 1) < 0.001
Example #6
0
def test_characterisation(qvm: None, quilc: None) -> None:
    b = ForestBackend("9q-square")
    char = b.characterisation
    assert char
    dev = Device(
        char.get("NodeErrors", {}),
        char.get("EdgeErrors", {}),
        char.get("Architecture", Architecture([])),
    )
    assert dev
Example #7
0
def test_measures(qvm: None, quilc: None) -> None:
    n_qbs = 9
    c = Circuit(n_qbs, n_qbs)
    x_qbs = [2, 5, 7, 8]
    for i in x_qbs:
        c.X(i)
    c.measure_all()
    b = ForestBackend("9q-square")
    b.compile_circuit(c)
    shots = b.get_shots(c, 10)
    print(shots)
    all_ones = True
    all_zeros = True
    for i in x_qbs:
        all_ones = all_ones and cast(bool, np.all(shots[:, i]))
    for i in range(n_qbs):
        if i not in x_qbs:
            all_zeros = all_zeros and (not np.any(shots[:, i]))
    assert all_ones
    assert all_zeros
Example #8
0
def test_operator_sim(qvm: None, quilc: None) -> None:
    c = Circuit(2, 2)
    c.Rz(0.5, 0)
    b = ForestBackend("9q-square")
    zi = QubitPauliString(Qubit(0), Pauli.Z)
    iz = QubitPauliString(Qubit(1), Pauli.Z)
    op = QubitPauliOperator({zi: 0.3, iz: -0.1})
    assert get_operator_expectation_value(c, op, b,
                                          10) == pytest.approx(0.2, rel=0.001)
    c.X(0)
    assert get_operator_expectation_value(c, op, b,
                                          10) == pytest.approx(-0.4, rel=0.001)
Example #9
0
def test_ilo(qvm: None, quilc: None) -> None:
    b = ForestBackend("9q-square")
    bs = ForestStateBackend()
    c = Circuit(2)
    c.CZ(0, 1)
    c.Rx(1.0, 1)
    assert np.allclose(bs.get_state(c), np.asarray([0, -1j, 0, 0]))
    assert np.allclose(bs.get_state(c, basis=BasisOrder.dlo),
                       np.asarray([0, 0, -1j, 0]))
    c.rename_units({Qubit(0): Node(0), Qubit(1): Node(1)})
    c.measure_all()
    assert (b.get_shots(c, 2) == np.asarray([[0, 1], [0, 1]])).all()
    assert (b.get_shots(c, 2,
                        basis=BasisOrder.dlo) == np.asarray([[1, 0],
                                                             [1, 0]])).all()
    assert b.get_counts(c, 2) == {(0, 1): 2}
    assert b.get_counts(c, 2, basis=BasisOrder.dlo) == {(1, 0): 2}
Example #10
0
#
# Both of these still have a few restrictions on the circuits that can be run. Each only supports a subset of the gate types available in `pytket`, and a real device or associated simulation will have restricted qubit connectivity. The Backend objects will contain a default compilation pass that will statisfy these constraints as much as possible, with minimal or no optimisation.
#
# The `ForestStateBackend` will allow us to view the full statevector (wavefunction) expected from a perfect execution of the circuit.

from pytket.extensions.pyquil import ForestStateBackend

state_backend = ForestStateBackend()
state_backend.compile_circuit(tk_circ)

state = state_backend.get_state(tk_circ)
print(state)

# For users who are familiar with the Forest SDK, the association of qubits to indices of bitstrings (and consequently the ordering of statevectors) used by default in `pytket` Backends differs from that described in the [Forest docs](http://docs.rigetti.com/en/stable/wavefunction_simulator.html#multi-qubit-basis-enumeration). You can recover the ordering used by the Forest systems with `state_backend.get_state(tk_circ, basis:pytket.BasisOrder.dlo)` (see our docs on the `BasisOrder` enum for more details).

# Connecting to real devices works very similarly. Instead of obtaining the full statevector, we are only able to measure the quantum state and sample from the resulting distribution. Beyond that, the process is pretty much the same.
#
# The following shows how to run the circuit on the "9q-square" lattice. The `simulator` switch on the `ForestBackend` will switch between connecting to the real Aspen device and the QVM, allowing you to test your code with a simulator before you reserve your slot with the device.

tk_circ.measure_all()

from pytket.extensions.pyquil import ForestBackend

aspen_backend = ForestBackend("9q-square", simulator=True)
aspen_backend.compile_circuit(tk_circ)

counts = aspen_backend.get_counts(tk_circ, 2000)
print(counts)

# Note that attempting to connect to a live quantum device (using a `ForestBackend` with `as_simulator=False`) will fail unless it is running from a QMI instance during a reservation for the named lattice.
Example #11
0
def test_sim(qvm: None, quilc: None) -> None:
    c = circuit_gen(True)
    b = ForestBackend("9q-square")
    b.compile_circuit(c)
    shots = b.get_shots(c, 1024)
    print(shots)
Example #12
0
def test_counts(qvm: None, quilc: None) -> None:
    c = circuit_gen(True)
    b = ForestBackend("9q-square")
    b.compile_circuit(c)
    counts = b.get_counts(c, 10)
    assert all(x[0] == x[1] for x in counts.keys())