Ejemplo n.º 1
0
def test_to_numpy_matrix():
    # Only numbers
    phi = (1 / np.sqrt(2)) * (BaseQubitState("00").to_state() +
                              BaseQubitState("11").to_state())
    op = outer_product(phi, phi)
    expected = np.array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                         [1, 0, 0, 1]]) / 2
    print(op)
    print(op.to_numpy_matrix())
    assert np.all(np.isclose(op.to_numpy_matrix(), expected))

    # Symbolic scalars
    xy = InnerProductFunction("x", "y")
    phi = xy * (BaseQubitState("00").to_state() +
                BaseQubitState("11").to_state())
    op = outer_product(phi, phi)
    expected = np.array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                         [1, 0, 0, 1]]) / 2

    def convert_scalars(scalar):
        return 1 / 2

    print(op)
    with pytest.raises(ValueError):
        op.to_numpy_matrix()
    m = op.to_numpy_matrix(convert_scalars=convert_scalars)
    print(m)
    assert np.all(np.isclose(m, expected))
Ejemplo n.º 2
0
def test_operator_dagger():
    # Y (hermitian)
    s0 = BaseQubitState("0").to_state()
    s1 = BaseQubitState("1").to_state()
    Y = outer_product(s1, s0) * 1j + outer_product(s0, s1) * (-1j)
    print(Y)
    assert Y == Y.dagger()

    # T (non-hermitian)
    s0 = BaseQubitState("0").to_state()
    s1 = BaseQubitState("1").to_state()
    T = outer_product(s0, s0) + outer_product(s1, s1) * np.exp(np.pi * 1j / 4)
    print(T)
    assert T != T.dagger()
Ejemplo n.º 3
0
def test_to_operator():
    bs0 = BaseQubitState("0")
    s0 = bs0.to_state()
    bop0 = BaseOperator(bs0, bs0)
    print(bop0.to_operator())
    print(Operator([bop0]))
    assert bop0.to_operator() == Operator([bop0])
    assert bop0.to_operator() == outer_product(s0, s0)
Ejemplo n.º 4
0
def test_mul_state():
    z0 = BaseQubitState("0").to_state()
    z1 = BaseQubitState("1").to_state()
    x0 = (z0 + z1) * (1 / np.sqrt(2))
    x1 = (z0 - z1) * (1 / np.sqrt(2))
    H = outer_product(x0, z0) + outer_product(x1, z1)

    # Check the inner product after applying H
    assert np.isclose((H * z0).inner_product(z0), 1 / np.sqrt(2))
    assert np.isclose((H * z0).inner_product(z1), 1 / np.sqrt(2))
    assert np.isclose((H * z0).inner_product(x0), 1)
    assert np.isclose((H * z0).inner_product(x1), 0)

    assert np.isclose((H * x0).inner_product(z0), 1)
    assert np.isclose((H * x0).inner_product(z1), 0)
    assert np.isclose((H * x0).inner_product(x0), 1 / np.sqrt(2))
    assert np.isclose((H * x0).inner_product(x1), 1 / np.sqrt(2))
Ejemplo n.º 5
0
def construct_projector(num_left, num_right):
    """Construct the projector (eq. (46)-(51) in https://arxiv.org/abs/1903.09778)"""
    vac = BaseFockState([]).to_state()
    c1 = BaseFockState([FockOp('c', 'p1')]).to_state()
    c2 = BaseFockState([FockOp('c', 'p2')]).to_state()
    d1 = BaseFockState([FockOp('d', 'p1')]).to_state()
    d2 = BaseFockState([FockOp('d', 'p2')]).to_state()

    if (num_left, num_right) == (0, 0):
        # P00
        return outer_product(vac, vac)
    elif (num_left, num_right) == (1, 0):
        # P10
        return outer_product(c1, c1)
    elif (num_left, num_right) == (0, 1):
        # P01
        return outer_product(d1, d1)
    elif (num_left, num_right) == (1, 1):
        # P11
        return outer_product(c1 @ d2, c1 @ d2)
    elif (num_left, num_right) == (2, 0):
        # P20
        return outer_product(c1 @ c2, c1 @ c2)
    elif (num_left, num_right) == (0, 2):
        # P02
        return outer_product(d1 @ d2, d1 @ d2)
    else:
        raise NotImplementedError()
Ejemplo n.º 6
0
def test_measurement():
    s0 = BaseQubitState("0").to_state()
    s1 = BaseQubitState("1").to_state()
    h0 = (s0 + s1) * (1 / np.sqrt(2))

    P0 = outer_product(s0, s0)
    P1 = outer_product(s1, s1)
    kraus_ops = {0: P0, 1: P1}

    # Measure |0>
    meas_res = measure(s0, kraus_ops)
    assert meas_res.outcome == 0
    assert np.isclose(meas_res.probability, 1)

    # Measure |1>
    meas_res = measure(s1, kraus_ops)
    assert meas_res.outcome == 1
    assert np.isclose(meas_res.probability, 1)

    # Measure |+>
    meas_res = measure(h0, kraus_ops)
    assert np.isclose(meas_res.probability, 1 / 2)
Ejemplo n.º 7
0
def construct_beam_splitter():
    """Construct the beam splitter operation (eq. (40)-(41) in https://arxiv.org/abs/1903.09778)"""
    fock_states = list(get_fock_states())
    for i, state in enumerate(fock_states):
        for old, new in zip(['w1', 'w2'], ['b1', 'b2']):
            state = replace_var(state, old, new)
        fock_states[i] = state
    qubit_states = [
        BaseQubitState(b).to_state() for b in ["00", "01", "10", "11"]
    ]

    beam_splitter = sum(
        (outer_product(fock_state, qubit_state)
         for fock_state, qubit_state in zip(fock_states, qubit_states)),
        Operator())

    return beam_splitter.simplify()
Ejemplo n.º 8
0
def test_non_complete_kraus():
    s0 = BaseQubitState("0").to_state()
    s1 = BaseQubitState("1").to_state()
    op = outer_product(s0, s0)
    with pytest.raises(ValueError):
        measure(s1, {"0": op})
Ejemplo n.º 9
0
def test_mul_operator_state():
    s0 = BaseQubitState("0").to_state()
    s1 = BaseQubitState("1").to_state()
    splus = (s0 + s1) * (1 / np.sqrt(2))
    op0 = outer_product(s0, s0)
    assert s0 * (1 / np.sqrt(2)) == op0 * splus
Ejemplo n.º 10
0
def test_mul_operator_operator():
    s0 = BaseQubitState("0").to_state()
    op0 = outer_product(s0, s0)
    assert op0 == op0 * op0