def test_ilo() -> None:
    b = AerBackend()
    bs = AerStateBackend()
    bu = AerUnitaryBackend()
    c = Circuit(2)
    c.X(1)
    assert (bs.get_state(c) == np.asarray([0, 1, 0, 0])).all()
    assert (bs.get_state(c, basis=BasisOrder.dlo) == np.asarray([0, 0, 1,
                                                                 0])).all()
    assert (bu.get_unitary(c) == np.asarray([[0, 1, 0, 0], [1, 0, 0, 0],
                                             [0, 0, 0, 1], [0, 0, 1,
                                                            0]])).all()
    assert (bu.get_unitary(c,
                           basis=BasisOrder.dlo) == np.asarray([[0, 0, 1, 0],
                                                                [0, 0, 0, 1],
                                                                [1, 0, 0, 0],
                                                                [0, 1, 0,
                                                                 0]])).all()
    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}
def test_cache() -> None:
    b = AerBackend()
    c = circuit_gen()
    b.compile_circuit(c)
    h = b.process_circuits([c], 2)[0]
    b.get_result(h).get_shots()
    assert h in b._cache
    b.pop_result(h)
    assert h not in b._cache
    assert not b._cache

    b.get_counts(c, n_shots=2)
    b.get_counts(c.copy(), n_shots=2)
    b.empty_cache()
    assert not b._cache
def test_simulation_method() -> None:
    state_backends = [
        AerBackend(),
        AerBackend(simulation_method="statevector")
    ]
    stabilizer_backend = AerBackend(simulation_method="stabilizer")

    clifford_circ = Circuit(2).H(0).CX(0, 1).measure_all()
    clifford_T_circ = Circuit(2).H(0).T(1).CX(0, 1).measure_all()

    for b in state_backends + [stabilizer_backend]:
        counts = b.get_counts(clifford_circ, 4)
        assert sum(val for _, val in counts.items()) == 4

    for b in state_backends:
        counts = b.get_counts(clifford_T_circ, 4)
        assert sum(val for _, val in counts.items()) == 4

    with pytest.raises(AttributeError) as warninfo:
        # check for the error thrown when non-clifford circuit used with
        # stabilizer backend
        stabilizer_backend.get_counts(clifford_T_circ, 4)
        assert "Attribute header is not defined" in str(warninfo.value)
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)}
def test_shots_bits_edgecases(n_shots: int, n_bits: int) -> None:
    c = Circuit(n_bits, n_bits)
    aer_backend = AerBackend()

    # TODO TKET-813 add more shot based backends and move to integration tests
    h = aer_backend.process_circuit(c, n_shots)
    res = aer_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(aer_backend.get_shots(c, n_shots), correct_shots)
    assert aer_backend.get_shots(c, n_shots).shape == correct_shape
    assert aer_backend.get_counts(c, n_shots) == correct_counts
def test_counts() -> None:
    qc = get_test_circuit(True)
    circ = qiskit_to_tk(qc)
    sim = AerBackend()
    counts = sim.get_counts(circ, 10, seed=4)
    assert counts == {(1, 0, 1, 1, 0): 10}
示例#7
0
sim_handles = pytket_noisy_sim_backend.process_circuits(
    calibration_circuits, n_shots)

# Count results from the simulator are then used to calculate the matrices used for SPAM correction for ```ibmq_santiago```.

sim_count_results = (pytket_noisy_sim_backend.get_result(handle).get_counts()
                     for handle in sim_handles)
santiago_spam.calculate_matrices(sim_count_results)

from pytket import Circuit

ghz_circuit = (Circuit(len(pytket_santiago_device.nodes)).H(0).CX(0, 1).CX(
    1, 2).measure_all())
pytket_noisy_sim_backend.compile_circuit(ghz_circuit)
ghz_noisy_counts = pytket_noisy_sim_backend.get_counts(ghz_circuit, n_shots)

# We also run a noiseless simulation so we can compare performance.

pytket_noiseless_sim_backend = AerBackend()
ghz_noiseless_counts = pytket_noiseless_sim_backend.get_counts(
    ghz_circuit, n_shots)

# Noisy simulator counts are corrected using the ```SpamCorrecter``` objects ```correct_counts``` method.
#
# To correctly amend counts, the ```correct_counts``` method requires the executed circuits qubit_readout, a map from qubit to its index in readouts from backends.

ghz_spam_corrected_counts = santiago_spam.correct_counts(
    ghz_noisy_counts, ghz_circuit.qubit_readout)

# Import and define the Jensen-Shannon divergence, which we will use for comparing performance. The Jensen-Shannon divergence is a symmetric and finite measure of similarity between two probability distributions. A smaller divergence implies more similarity between two probability distributions.