Beispiel #1
0
def test_eq():
    bs0 = BaseQubitState("0")
    bs1 = BaseQubitState("1")

    assert BaseOperator(bs0, bs0) == BaseOperator(bs0, bs0)
    assert BaseOperator(bs0, bs1) != BaseOperator(bs1, bs0)
    assert BaseOperator(bs0, bs0) != BaseOperator(bs1, bs1)
Beispiel #2
0
def test_init(input, error):
    if error is not None:
        with pytest.raises(error):
            BaseQubitState(input)
    else:
        s = BaseQubitState(input)
        assert s._digits == input
Beispiel #3
0
def test_mul():
    s = BaseQubitState("00").to_state() + BaseQubitState("11").to_state()
    x = 1 / np.sqrt(2)
    s2 = s * x
    s3 = x * s
    assert str(s2) == f"{x}*|00> + {x}*|11>"
    assert str(s3) == f"{x}*|00> + {x}*|11>"
Beispiel #4
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))
Beispiel #5
0
def test_non_complete_mul():
    bs0 = BaseQubitState("0")
    s00 = BaseQubitState("00").to_state()
    bop = BaseOperator(bs0, bs0)
    with pytest.raises(TypeError):
        bop * s00
    with pytest.raises(TypeError):
        bop * 1
Beispiel #6
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)
Beispiel #7
0
def test_simplify_operator():
    bs0 = BaseQubitState("0")
    bs1 = BaseQubitState("1")
    op = Operator([BaseOperator(bs0, bs0)])
    op._terms[BaseOperator(bs1, bs1)] = 0

    assert len(op) == 2
    op = simplify(op)
    assert len(op) == 1
Beispiel #8
0
def test_tensor_product():
    s = (BaseQubitState('0').to_state() +
         BaseQubitState('1').to_state()) * (1 / np.sqrt(2))
    s_tensor = s @ s
    expected = sum((BaseQubitState(f"{i}{j}").to_state() for i in range(2)
                    for j in range(2)), State())
    expected *= 1 / 2

    expected == s_tensor
Beispiel #9
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()
Beispiel #10
0
def test_faulty_init_operator():
    bs0 = BaseQubitState("0")
    bop0 = BaseOperator(bs0, bs0)
    with pytest.raises(ValueError):
        Operator([bop0], [1, 2])  # Wrong number of scalars

    with pytest.raises(TypeError):
        Operator([bs0], [1])  # Not a base operator

    with pytest.raises(TypeError):
        Operator([bop0], [bs0])  # Not a scalar

    bs00 = BaseQubitState("00")
    bop00 = BaseOperator(bs00, bs00)
    with pytest.raises(ValueError):
        Operator([bop0, bop00])  # Not add compatible
Beispiel #11
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))
Beispiel #12
0
def test_simplify():
    prod = ProductOfScalars()
    prod._factors = [2, 3, 5]

    bs = BaseQubitState('0')
    s = State([bs], scalars=[prod])
    assert s.get_scalar(bs) != 30
    s = simplify(s)
    assert s.get_scalar(bs) == 30
Beispiel #13
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)
Beispiel #14
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()
Beispiel #15
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})
Beispiel #16
0
    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)


@pytest.mark.parametrize("state, kraus_ops, error", [
    (None, {}, TypeError),
    (BaseQubitState("0").to_state(), None, TypeError),
    (BaseQubitState("0").to_state(), {
        "0": 0,
        "1": 1
    }, TypeError),
])
def test_faulty(state, kraus_ops, error):
    with pytest.raises(error):
        measure(state, kraus_ops)


def test_non_complete_kraus():
    s0 = BaseQubitState("0").to_state()
    s1 = BaseQubitState("1").to_state()
    op = outer_product(s0, s0)
    with pytest.raises(ValueError):
Beispiel #17
0
def test_mul_operator_operator():
    s0 = BaseQubitState("0").to_state()
    op0 = outer_product(s0, s0)
    assert op0 == op0 * op0
Beispiel #18
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
Beispiel #19
0
import pytest
import numpy as np

from qualg.states import State
from qualg.q_state import BaseQubitState
from qualg.toolbox import simplify
from qualg.scalars import ProductOfScalars


@pytest.mark.parametrize("input, scalars, num_terms, error", [
    (BaseQubitState("0"), None, None, TypeError),
    ((1, BaseQubitState("0")), None, None, TypeError),
    ([(None, BaseQubitState("0"))], None, None, TypeError),
    ([(1, None)], None, None, TypeError),
    ([BaseQubitState("0")], None, 1, None),
    ([
        BaseQubitState("0"),
        BaseQubitState("1"),
    ], None, 2, None),
    ([
        BaseQubitState("0"),
        BaseQubitState("0"),
    ], None, 1, None),
    ([
        BaseQubitState("0"),
        BaseQubitState("00"),
    ], None, None, ValueError),
    ([
        BaseQubitState("0"),
        BaseQubitState("1"),
    ], 1, None, TypeError),
Beispiel #20
0
    (None, TypeError),
    (1.0, TypeError),
    ("012", ValueError),
    ("O1O", ValueError),  # letter O instead of digit 0
])
def test_init(input, error):
    if error is not None:
        with pytest.raises(error):
            BaseQubitState(input)
    else:
        s = BaseQubitState(input)
        assert s._digits == input


@pytest.mark.parametrize("state1, state2, expected, error", [
    (BaseQubitState("0"), BaseQubitState("0"), True, None),
    (BaseQubitState("1"), BaseQubitState("0"), False, None),
    (BaseQubitState("0000"), BaseQubitState("0"), False, None),
    (BaseQubitState("0000"), BaseQubitState("0000"), True, None),
    (BaseQubitState("0001"), BaseQubitState("0000"), False, None),
    (BaseQubitState("0000"), "0000", False, None),
])
def test_eq(state1, state2, expected, error):
    if error is not None:
        print(state1 == state2)
        with pytest.raises(error):
            state1 == state2
    else:
        assert (state1 == state2) == expected

Beispiel #21
0
def test_mul_operator_scalar():
    bs0 = BaseQubitState("0")
    bop0 = BaseOperator(bs0, bs0)
    op = Operator([bop0])
    assert op * 0.5 == Operator([bop0], [0.5])