コード例 #1
0
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}
コード例 #2
0
def test_statevector() -> None:
    c = circuit_gen()
    b = AerStateBackend()
    state = b.get_state(c)
    assert np.allclose(
        state, [math.sqrt(0.5), 0, 0, math.sqrt(0.5)], atol=1e-10)
    c.add_phase(0.5)
    state1 = b.get_state(c)
    assert np.allclose(state1, state * 1j, atol=1e-10)
コード例 #3
0
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)
コード例 #4
0
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)