def test_del_and_circuit():
    with qp.Sim(enforce_release_at_zero=False, phase_fixup_bias=True):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='q') as q:
                q[0].clear(q[1] & q[2])

    cirq.testing.assert_has_diagram(circuit, r"""
q[0]: ---alloc---Mxc-------cxM---release---
         |                       |
q[1]: ---alloc---------@---------release---
         |             |         |
q[2]: ---alloc---------Z---------release---
        """, use_unicode_characters=False)

    with qp.Sim(enforce_release_at_zero=False, phase_fixup_bias=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='b') as q:
                q[0].clear(q[1] & q[2])

    cirq.testing.assert_has_diagram(circuit, r"""
b[0]: ---alloc---Mxc---cxM---release---
         |                   |
b[1]: ---alloc---------------release---
         |                   |
b[2]: ---alloc---------------release---
        """, use_unicode_characters=False)
Ejemplo n.º 2
0
def test_del_unary_circuit():
    with qp.Sim(phase_fixup_bias=True):
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            (1 << b).clear_storage_location(u, c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -------------------------------------------------------------------------------------------------------------------------------------------------------Z---------

b[0]: -------------------------------------------------------------------------------------------------------------------------------------Z-------------------------
                                                                                                                                           |
b[1]: ---------------------------------------------------------------------------------------------Z-------------------Z-------------------|-------------------------
                                                                                                   |                   |                   |
b[2]: -------------Z-------------------Z-------------------Z-------------------Z-------------------|-------------------|-------------------|-------------------------
                   |                   |                   |                   |                   |                   |                   |
u[0]: -------------|-------------------|-------------------|---------X---------@-------------------|---------X---------@---------X---------@---------Mxc-------cxM---
                   |                   |                   |         |                             |         |                   |
u[1]: -------------|-------------------|---------X---------@---------|-------------------X---------@---------|-------------------@---Mxc-------cxM-------------------
                   |                   |         |                   |                   |                   |
u[2]: -------------|---------X---------@---------|-------------------|-------------------|-------------------@---Mxc-------cxM---------------------------------------
                   |         |                   |                   |                   |
u[3]: ---X---------@---------|-------------------|-------------------|-------------------@---Mxc-------cxM-----------------------------------------------------------
         |                   |                   |                   |
u[4]: ---|-------------------|-------------------|-------------------@---Mxc-------cxM-------------------------------------------------------------------------------
         |                   |                   |
u[5]: ---|-------------------|-------------------@---Mxc-------cxM---------------------------------------------------------------------------------------------------
         |                   |
u[6]: ---|-------------------@---Mxc-------cxM-----------------------------------------------------------------------------------------------------------------------
         |
u[7]: ---@---Mxc-------cxM-------------------------------------------------------------------------------------------------------------------------------------------
""", use_unicode_characters=False)

    with qp.Sim(phase_fixup_bias=False):
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            (1 << b).clear_storage_location(u, c)

    cirq.testing.assert_has_diagram(circuit, r"""
u[0]: ---------------------------------------------------X-------------------------------X---------------X---------------Mxc---cxM---
                                                         |                               |               |
u[1]: -----------------------------------X---------------|---------------X---------------|---------------@---Mxc---cxM---------------
                                         |               |               |               |
u[2]: -------------------X---------------|---------------|---------------|---------------@---Mxc---cxM-------------------------------
                         |               |               |               |
u[3]: ---X---------------|---------------|---------------|---------------@---Mxc---cxM-----------------------------------------------
         |               |               |               |
u[4]: ---|---------------|---------------|---------------@---Mxc---cxM---------------------------------------------------------------
         |               |               |
u[5]: ---|---------------|---------------@---Mxc---cxM-------------------------------------------------------------------------------
         |               |
u[6]: ---|---------------@---Mxc---cxM-----------------------------------------------------------------------------------------------
         |
u[7]: ---@---Mxc---cxM---------------------------------------------------------------------------------------------------------------
        """, use_unicode_characters=False)
def test_optional():
    def cf(x: qp.IntBuf, y: bool = True):
        x ^= int(y)

    @qp.semi_quantum(classical=cf)
    def qf(x: qp.Qubit, y: qp.Qubit.Borrowed = True):
        x ^= y

    b = qp.IntBuf.raw(val=0, length=1)
    qf.classical(b)
    assert int(b) == 1
    qf.classical(b)
    assert int(b) == 0
    qf.classical(b, True)
    assert int(b) == 1
    qf.classical(b, False)
    assert int(b) == 1

    with qp.Sim() as sim_state:
        q = qp.qalloc()
        assert not sim_state.resolve_location(q, False)
        qf(q)
        assert sim_state.resolve_location(q, False)
        qf(q)
        assert not sim_state.resolve_location(q, False)
        qf(q, True)
        assert sim_state.resolve_location(q, False)
        qf(q, False)
        assert sim_state.resolve_location(q, False)
        assert qp.measure(q, reset=True)
        qp.qfree(q)
def test_init_square_circuit():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        factor = qp.qalloc(len=2, name='f')
        out = qp.qalloc(len=4, name='s')
        with qp.LogCirqCircuit() as circuit:
            init_square(factor=factor, clean_out=out)
    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_do_addition_carry_in: -----------------------alloc---X-------@---------------------------------------------------------------@---@-------X---Mxc---cxM---release-----------------alloc---X-------@-------------------------------@---@-------X---Mxc---cxM---release-------------
                                                      |       |                                                               |   |       |                                               |       |                               |   |       |
_sqr_offset: ---------------------alloc---X-----------|-------|---------------@---@---X---@---X-------@---@-------------------|---|-------|-------------------------X---release-----------|-------|-------------------------------|---|-------|-----------------------------------
                                          |           |       |               |   |   |   |   |       |   |                   |   |       |                         |                     |       |                               |   |       |
_sqr_zero: ---------------alloc-----------|-----------|-------|---@---@---X---X---|---@---|---@---@---|---X---X-------@---@---|---|-------|-------------------------|---------------------|-------|---@---@---X---X-------@---@---|---|-------|-------------------------release---
                                          |           |       |   |   |   |       |   |   |   |   |   |       |       |   |   |   |       |                         |                     |       |   |   |   |   |       |   |   |   |       |
f[0]: ------------------------------------@-----------@---@---X---X---|---@-------|---|---|---|---|---|-------@---@---|---X---X---|---@---@-------------------------@---------------------|-------|---|---|---|---|-------|---|---|---|-------|-----------------------------------
                                          |               |   |       |   |       |   |   |   |   |   |       |   |   |       |   |   |                             |                     |       |   |   |   |   |       |   |   |   |       |
f[1]: ------------------------------------@---------------|---|-------|---|-------|---|---|---|---|---|-------|---|---|-------|---|---|-----------------------------@---------------------@---@---X---X---|---@---@---@---|---X---X---|---@---@-----------------------------------
                                                          |   |       |   |       |   |   |   |   |   |       |   |   |       |   |   |                                                       |   |       |   |   |   |   |       |   |   |
s[0]: ----------------------------------------------------X---@-------|---|-------|---|---|---|---|---|-------|---|---|-------@---X---X-------------------------------------------------------|---|-------|---|---|---|---|-------|---|---|---------------------------------------
                                                                      |   |       |   |   |   |   |   |       |   |   |                                                                       |   |       |   |   |   |   |       |   |   |
s[1]: ----------------------------------------------------------------X---@-------|---|---|---|---|---|-------@---X---X-----------------------------------------------------------------------|---|-------|---|---|---|---|-------|---|---|---------------------------------------
                                                                                  |   |   |   |   |   |                                                                                       |   |       |   |   |   |   |       |   |   |
s[2]: ----------------------------------------------------------------------------X---@---|---@---X---X---------------------------------------------------------------------------------------X---@-------|---|---|---|---|-------@---X---X---------------------------------------
                                                                                          |                                                                                                               |   |   |   |   |
s[3]: ------------------------------------------------------------------------------------X---------------------------------------------------------------------------------------------------------------X---@---@---X---X-------------------------------------------------------
""",
                                    use_unicode_characters=False)
Ejemplo n.º 5
0
def _apply_classical(func: Callable, kwargs: Dict[str, Any]) -> Dict[str, Any]:
    kwargs = {
        k: v.copy() if hasattr(v, 'copy') else v
        for k, v in kwargs.items()
    }

    type_hints = get_type_hints(func)
    if 'control' in kwargs and 'control' not in type_hints:
        run = kwargs['control']
        del kwargs['control']
    else:
        run = True

    if run:
        if 'sim_state' in type_hints:
            kwargs['sim_state'] = qp.Sim()
        func(**kwargs)

    result = {'sim_state.phase_degrees': 0}
    for k, v in kwargs.items():
        if isinstance(v, qp.IntBuf):
            result[k] = int(v)
        elif isinstance(v, qp.Sim):
            result['sim_state.phase_degrees'] = v.phase_degrees
    return result
Ejemplo n.º 6
0
def test_let_unary_circuit():
    with qp.Sim():
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            (1 << b).init_storage_location(u, c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -----@-----------------------------------------------------------
         |
b[0]: ---|---@-------------------------------------------------------
         |   |
b[1]: ---|---|-------@-------@---------------------------------------
         |   |       |       |
b[2]: ---|---|-------|-------|-------@-------@-------@-------@-------
         |   |       |       |       |       |       |       |
u[0]: ---X---@---X---@---X---|-------@---X---|-------|-------|-------
             |   |   |   |   |       |   |   |       |       |
u[1]: -------X---@---|---|---@---X---|---|---@---X---|-------|-------
                     |   |   |   |   |   |   |   |   |       |
u[2]: ---------------X---@---|---|---|---|---|---|---@---X---|-------
                             |   |   |   |   |   |   |   |   |
u[3]: -----------------------X---@---|---|---|---|---|---|---@---X---
                                     |   |   |   |   |   |   |   |
u[4]: -------------------------------X---@---|---|---|---|---|---|---
                                             |   |   |   |   |   |
u[5]: ---------------------------------------X---@---|---|---|---|---
                                                     |   |   |   |
u[6]: -----------------------------------------------X---@---|---|---
                                                             |   |
u[7]: -------------------------------------------------------X---@---
        """, use_unicode_characters=False)
Ejemplo n.º 7
0
def test_plus_equal_gate_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        a = qp.qalloc(len=3, name='a')
        t = qp.qalloc(len=4, name='t')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            qp.arithmetic.do_addition(lvalue=t, offset=a, carry_in=c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -----X-------@---------------------------------------------------------------@---@-------X---
         |       |                                                               |   |       |
a[0]: ---@---@---X---X-------@-----------------------------------@---@-------X---X---|---@---@---
             |   |   |       |                                   |   |       |   |   |   |
a[1]: -------|---|---@---@---X---X-------@-------@---@-------X---X---|---@---@---|---|---|-------
             |   |       |   |   |       |       |   |       |   |   |   |       |   |   |
a[2]: -------|---|-------|---|---@---@---X---@---X---|---@---@---|---|---|-------|---|---|-------
             |   |       |   |       |   |   |   |   |   |       |   |   |       |   |   |
t[0]: -------X---@-------|---|-------|---|---|---|---|---|-------|---|---|-------@---X---X-------
                         |   |       |   |   |   |   |   |       |   |   |
t[1]: -------------------X---@-------|---|---|---|---|---|-------@---X---X-----------------------
                                     |   |   |   |   |   |
t[2]: -------------------------------X---@---|---@---X---X---------------------------------------
                                             |
t[3]: ---------------------------------------X---------------------------------------------------
        """, use_unicode_characters=False)
Ejemplo n.º 8
0
def test_if_less_than_then_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        lhs = qp.qalloc(len=4, name='lhs')
        rhs = qp.qalloc(len=4, name='rhs')
        c = qp.qalloc(name='_or_eq')
        t = qp.qalloc(name='t')
        with qp.LogCirqCircuit() as circuit:
            qp.arithmetic.do_if_less_than(
                lhs=lhs,
                rhs=rhs,
                or_equal=c,
                effect=t.__ixor__)
    cirq.testing.assert_has_diagram(circuit, r"""
_or_eq: ---@---X---@-------------------------------------------------------------------------------@---X---@---
           |   |   |                                                                               |   |   |
lhs[0]: ---X---|---@-------------------------------------------------------------------------------@---|---X---
               |   |                                                                               |   |
lhs[1]: -------|---|---X-------@-------------------------------------------------------@-------X---|---|-------
               |   |   |       |                                                       |       |   |   |
lhs[2]: -------|---|---|-------|---X-------@-------------------------------@-------X---|-------|---|---|-------
               |   |   |       |   |       |                               |       |   |       |   |   |
lhs[3]: -------|---|---|-------|---|-------|---X-------@-------@-------X---|-------|---|-------|---|---|-------
               |   |   |       |   |       |   |       |       |       |   |       |   |       |   |   |
rhs[0]: -------@---X---@---X---@---|-------|---|-------|-------|-------|---|-------|---@---X---@---X---@-------
                           |   |   |       |   |       |       |       |   |       |   |   |
rhs[1]: -------------------@---X---@---X---@---|-------|-------|-------|---@---X---@---X---@-------------------
                                       |   |   |       |       |       |   |   |
rhs[2]: -------------------------------@---X---@---X---@-------@---X---@---X---@-------------------------------
                                                   |   |       |   |
rhs[3]: -------------------------------------------@---X---@---X---@-------------------------------------------
                                                           |
t: --------------------------------------------------------X---------------------------------------------------
        """, use_unicode_characters=False)
Ejemplo n.º 9
0
def test_xor_equal_gate_circuit_2():
    with qp.Sim(enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='a') as a:
                with qp.qalloc(len=4, name='t') as t:
                    with qp.qalloc(name='_c') as c:
                        t ^= a
                        t ^= a & qp.controlled_by(c)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_c: ---------------------alloc---------------@---@---@---release-----------------------
                                             |   |   |
a[0]: ---alloc-------------------@-----------@---|---|-----------------------release---
         |                       |           |   |   |                       |
a[1]: ---alloc-------------------|---@-------|---@---|-----------------------release---
         |                       |   |       |   |   |                       |
a[2]: ---alloc-------------------|---|---@---|---|---@-----------------------release---
                                 |   |   |   |   |   |
t[0]: -----------alloc-----------X---|---|---X---|---|-------------release-------------
                 |                   |   |       |   |             |
t[1]: -----------alloc---------------X---|-------X---|-------------release-------------
                 |                       |           |             |
t[2]: -----------alloc-------------------X-----------X-------------release-------------
                 |                                                 |
t[3]: -----------alloc---------------------------------------------release-------------
        """,
                                    use_unicode_characters=False)
Ejemplo n.º 10
0
def _apply_quantum(func: Callable, kwargs: Dict[str, Any]) -> Dict[str, Any]:
    with qp.Sim() as sim:
        type_hints = get_type_hints(func)
        mapped = {}
        for k, v in kwargs.items():
            if type_hints[k] == qp.Quint:
                assert isinstance(
                    v, qp.IntBuf
                ), 'Expected type qp.IntBuf for argument "{}"'.format(k)
                mapped[k] = qp.qalloc(len=len(v), name=k)
                mapped[k] ^= int(v)
            elif type_hints[k] == qp.Qubit:
                assert isinstance(v, qp.IntBuf) and len(
                    v
                ) == 1, 'Expected length 1 qp.IntBuf for argument "{}"'.format(
                    k)
                mapped[k] = qp.qalloc(name=k)
                mapped[k] ^= int(v)
            else:
                mapped[k] = v

        func(**mapped)

        result = {}
        for k, v in kwargs.items():
            if type_hints[k] in [qp.Quint, qp.Qubit]:
                result[k] = qp.measure(mapped[k], reset=True)
        result['sim_state.phase_degrees'] = sim.phase_degrees
    return result
Ejemplo n.º 11
0
def test_eq():
    with qp.Sim():
        with qp.qalloc(len=4) as t:
            t.init(5)
            for k in range(-2, 60):
                assert qp.measure(t == k) == (k == 5)
            assert qp.measure(t, reset=True) == 5
Ejemplo n.º 12
0
def test_xor_equal_gate_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='a') as a:
                with qp.qalloc(len=4, name='t') as t:
                    with qp.qalloc(name='_c') as c:
                        qp.arithmetic.do_xor(lvalue=t, mask=a)
                        qp.arithmetic.do_xor(lvalue=t, mask=a, control=c)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_c: ---------------------alloc---------------@---@---@---release-----------------------
                                             |   |   |
a[0]: ---alloc-------------------@-----------@---|---|-----------------------release---
         |                       |           |   |   |                       |
a[1]: ---alloc-------------------|---@-------|---@---|-----------------------release---
         |                       |   |       |   |   |                       |
a[2]: ---alloc-------------------|---|---@---|---|---@-----------------------release---
                                 |   |   |   |   |   |
t[0]: -----------alloc-----------X---|---|---X---|---|-------------release-------------
                 |                   |   |       |   |             |
t[1]: -----------alloc---------------X---|-------X---|-------------release-------------
                 |                       |           |             |
t[2]: -----------alloc-------------------X-----------X-------------release-------------
                 |                                                 |
t[3]: -----------alloc---------------------------------------------release-------------
        """,
                                    use_unicode_characters=False)
Ejemplo n.º 13
0
def test_neq():
    with qp.Sim():
        with qp.qalloc(len=4) as t:
            t.init(5)
            for k in range(-2, 60):
                with qp.hold(t != k) as q:
                    assert qp.measure(q) == (k != 5)
            assert qp.measure(t, reset=True) == 5
def test_uncompute():
    for a, b, p in itertools.product([False, True], repeat=3):
        with qp.Sim(phase_fixup_bias=p):
            with qp.hold(a, name='a') as qa:
                with qp.hold(b, name='b') as qb:
                    with qp.hold(a & b, name='c') as qc:
                        assert qp.measure(qa) == a
                        assert qp.measure(qb) == b
                        assert qp.measure(qc) == (a and b)
Ejemplo n.º 15
0
def test_cmp():
    with qp.Sim():
        with qp.qalloc(len=4) as t:
            t.init(5)
            for k in range(-5, 30):
                assert qp.measure(t >= k) == (5 >= k)
                assert qp.measure(t > k) == (5 > k)
                assert qp.measure(t < k) == (5 < k)
                assert qp.measure(k < t) == (k < 5)
                assert qp.measure(t <= k) == (5 <= k)
            assert qp.measure(t, reset=True) == 5
Ejemplo n.º 16
0
def test_sim():
    v1 = 15
    v2 = 235
    offset = 4
    bits = 10
    with qp.Sim():
        with qp.hold(val=v1, name='a') as a:
            with qp.qalloc(len=bits, name='out') as out:
                out += a * v2
                out += offset
                result = qp.measure(out, reset=True)
    assert result == (v1*v2 + offset) & ((1 << bits) - 1)
def test_integration(exp_len: int, modulus_len: int, emulate_additions: bool):
    with qp.Sim(emulate_additions=emulate_additions):
        exponent = random.randint(0, 1 << exp_len)
        modulus = random.randint(0, 1 << modulus_len) * 2 + 1
        base = 0
        while math.gcd(base, modulus) != 1 or base >= modulus:
            base = random.randint(0, 1 << modulus_len) * 2 + 1

        with qp.hold(exponent) as exp:
            actual = measure_pow_mod(base=base, exponent=exp, modulus=modulus)

    expected = pow(base, exponent, modulus)
    assert actual == expected
def test_let_and_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='q') as q:
                q[0].init(q[1] & q[2])

    cirq.testing.assert_has_diagram(circuit, r"""
q[0]: ---alloc---X---release---
         |       |   |
q[1]: ---alloc---@---release---
         |       |   |
q[2]: ---alloc---@---release---
        """, use_unicode_characters=False)
def test_with_sim_state():
    def cf(sim_state: qp.ClassicalSimState):
        sim_state.phase_degrees += 180

    @qp.semi_quantum(classical=cf)
    def qf():
        qp.phase_flip()

    with qp.Sim() as sim_state:
        assert sim_state.phase_degrees == 0
        qf()
        assert sim_state.phase_degrees == 180
        qf.sim(sim_state)
        assert sim_state.phase_degrees == 0
Ejemplo n.º 20
0
def test_count():
    v1 = 15
    v2 = 235
    offset = 4
    bits = 100
    with qp.Sim():
        with qp.CountNots() as counts:
            with qp.hold(val=v1, name='a') as a:
                with qp.qalloc(len=bits, name='out') as out:
                    out += a * v2
                    out += offset
                    _ = qp.measure(out, reset=True)
    assert len(counts.keys()) == 3
    assert counts[0] > 0
    assert counts[1] > 0
    assert 0 < counts[2] <= 1000
Ejemplo n.º 21
0
def test_xor_lookup():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='addr') as addr:
                with qp.qalloc(len=8, name='out') as out:
                    with qp.qalloc(name='cnt') as cnt:
                        out ^= qp.LookupTable(range(
                            1, 17))[addr] & qp.controlled_by(cnt)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_lookup_prefix: -----------------------------alloc---X---X-----------@---@-------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------X-----------@---@-------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------Mxc-------cxM---release---------------------------------
                                                     |   |           |   |                                                                                                                                                             |                                                                                                                                                                   |                   |           |   |                                                                                                                                                             |                                                                                                                                                                   |
_lookup_prefix_1: -----------------------------------|---|---alloc---X---X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------Mxc---|---cxM---release---|---alloc---X---X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------Mxc---|---cxM---release-----------------------------------------------------------
                                                     |   |           |               |   |                                                         |                                                               |                               |   |                                                         |                                                               |                         |                   |           |               |   |                                                         |                                                               |                               |   |                                                         |                                                               |                         |
_lookup_prefix_2: -----------------------------------|---|-----------|-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release---------|-------------------|-----------|-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release---------|---------------------------------------------------------------------------
                                                     |   |           |               |               |   |       |             |                               |   |       |             |                         |                               |               |   |       |             |                               |   |       |             |                         |                         |                   |           |               |               |   |       |             |                               |   |       |             |                         |                               |               |   |       |             |                               |   |       |             |                         |                         |
_lookup_prefix_3: -----------------------------------|---|-----------|---------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------|-------------------|-----------|---------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------|---------------------------------------------------------------------------
                                                     |   |           |               |               |       |       |         |                               |       |       |         |                         |                               |               |       |       |         |                               |       |       |         |                         |                         |                   |           |               |               |       |       |         |                               |       |       |         |                         |                               |               |       |       |         |                               |       |       |         |                         |                         |
addr[0]: ------------alloc---------------------------|---|-----------|---------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------|-------------------|-----------|---------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------|-----------------------------------------------------------------release---
                     |                               |   |           |               |                       |       |                                                 |       |                                   |                               |                       |       |                                                 |       |                                   |                         |                   |           |               |                       |       |                                                 |       |                                   |                               |                       |       |                                                 |       |                                   |                         |                                                                 |
addr[1]: ------------alloc---------------------------|---|-----------|---------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------|-------------------|-----------|---------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------|-----------------------------------------------------------------release---
                     |                               |   |           |                                       |       |                                                 |       |                                                                                           |       |                                                 |       |                                                             |                   |           |                                       |       |                                                 |       |                                                                                           |       |                                                 |       |                                                             |                                                                 |
addr[2]: ------------alloc---------------------------|---|-----------@---------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------Z-------------------|-----------@---------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------Z-----------------------------------------------------------------release---
                     |                               |   |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                 |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                               |
addr[3]: ------------alloc---------------------------@---|---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------|---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------------Z---------------------------------------release---
                                                     |   |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                 |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                       |
cnt: --------------------------------alloc-----------@---@---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------@---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------------@-------------------release-----------------------
                                                                                                             |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |
out[0]: ---------------------alloc---------------------------------------------------------------------------X-------|-------------------------------------------------X-------|-------------------------------------------------------------------------------------------X-------|-------------------------------------------------X-------|-------------------------------------------------------------------------------------------------------------------------------------X-------|-------------------------------------------------X-------|-------------------------------------------------------------------------------------------X-------|-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                     |
out[1]: ---------------------alloc-----------------------------------------------------------------------------------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------|-------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------------------------------------------------|-------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------|-------X-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                 |                                                                                           |       |                                                 |       |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                     |
out[2]: ---------------------alloc---------------------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------X-------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                               |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                     |
out[3]: ---------------------alloc-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------X-------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                                                                                                     |
out[4]: ---------------------alloc---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
out[5]: ---------------------alloc-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
out[6]: ---------------------alloc-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
out[7]: ---------------------alloc-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------release-------------
        """,
                                    use_unicode_characters=False)
Ejemplo n.º 22
0
def sim_call(func: Callable,
             *args, **kwargs) -> 'qp.ArgsAndKwargs':

    def qalloc_as_needed(a: qp.ArgParameter):
        if a.parameter_type is None:
            raise ValueError('Function arguments must be annotated, so that '
                             'quantum values can be allocated if needed.')

        if a.parameter_type is qp.Quint:
            if isinstance(a.arg, qp.IntBuf):
                n = len(a.arg)
            elif isinstance(a.arg, int):
                n = a.arg.bit_length()
            else:
                raise ValueError('Unsupported Quint input: {}'.format(a))
            result = qp.qalloc(len=n, name=a.parameter.name)
            result.init(a.arg)
            return result

        if a.parameter_type is qp.QuintMod:
            assert isinstance(a.arg, ModInt)
            result = qp.qalloc(modulus=a.arg.modulus,
                               name=a.parameter.name)
            result.init(a.arg.val)
            return result

        if a.parameter_type is qp.Qubit:
            result = qp.qalloc(name=a.parameter.name)
            result.init(a.arg)
            return result

        return a.arg

    def qfree_as_needed(a: Any):
        if isinstance(a, (qp.Quint, qp.Qureg, qp.Qubit, qp.QuintMod)):
            result = qp.measure(a, reset=True)
            qp.qfree(a)
            return result
        return a

    matched = qp.ArgsAndKwargs(args, kwargs).match_parameters(func)
    with qp.Sim():
        allocated = matched.map(qalloc_as_needed)
        allocated.pass_into(func)
        return allocated.map(qfree_as_needed)
Ejemplo n.º 23
0
def test_eq_circuit():
    with qp.Sim(enforce_release_at_zero=False, phase_fixup_bias=True):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='lhs') as lhs:
                with qp.hold(lhs == 5, name='target'):
                    pass

    cirq.testing.assert_has_diagram(circuit, r"""
lhs[0]: ---alloc---------------@-----------------@-----------------------release---
           |                   |                 |                       |
lhs[1]: ---alloc-----------X---@---X---------X---@---X-------------------release---
           |               |   |   |         |   |   |                   |
lhs[2]: ---alloc-----------|---@---|---------|---@---|-------------------release---
           |               |   |   |         |   |   |                   |
lhs[3]: ---alloc-----------X---@---X---------X---Z---X-------------------release---
                               |
target: -----------alloc-------X-------Mxc---------------cxM---release-------------
        """, use_unicode_characters=False)
def test_classical():
    def g(x: qp.IntBuf, y: int):
        assert isinstance(x, qp.IntBuf)
        assert isinstance(y, int)
        x ^= y

    @qp.semi_quantum(classical=g)
    def f(x: qp.Quint, y: qp.Quint.Borrowed):
        x ^= y

    assert f.classical is g

    with qp.Sim() as sim_state:
        q = qp.qalloc(len=5)
        assert sim_state.resolve_location(q, False) == 0
        f.sim(sim_state, q, 3)
        assert sim_state.resolve_location(q, False) == 3
        f.sim(sim_state, q, 5)
        assert sim_state.resolve_location(q, False) == 6
        f.sim(sim_state, q, 6)
        qp.qfree(q)
Ejemplo n.º 25
0
def test_xor_unary_circuit():
    with qp.Sim(phase_fixup_bias=True):
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            u ^= (1 << b) & qp.controlled_by(c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -------------------------@---@-------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------
                             |   |                                                                                                                                                             |                                                                                                                                                                   |
_lookup_prefix: -----alloc---X---X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------Mxc---|---cxM---release---
                             |               |   |                                                         |                                                               |                               |   |                                                         |                                                               |                         |
_lookup_prefix_1: -----------|-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release---------|-------------------
                             |               |               |   |       |             |                               |   |       |             |                         |                               |               |   |       |             |                               |   |       |             |                         |                         |
_lookup_prefix_2: -----------|---------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------|-------------------
                             |               |               |       |       |         |                               |       |       |         |                         |                               |               |       |       |         |                               |       |       |         |                         |                         |
b[0]: -----------------------|---------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------|-------------------
                             |               |                       |       |                                                 |       |                                   |                               |                       |       |                                                 |       |                                   |                         |
b[1]: -----------------------|---------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------|-------------------
                             |                                       |       |                                                 |       |                                                                                           |       |                                                 |       |                                                             |
b[2]: -----------------------@---------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------Z-------------------
                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |
u[0]: ---------------------------------------------------------------X-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                             |                                                 |       |                                                                                           |       |                                                 |       |
u[1]: -----------------------------------------------------------------------X-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                               |       |                                                                                           |       |                                                 |       |
u[2]: -------------------------------------------------------------------------------------------------------------------------X-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                       |                                                                                           |       |                                                 |       |
u[3]: ---------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                   |       |                                                 |       |
u[4]: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                           |                                                 |       |
u[5]: -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                                                                             |       |
u[6]: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                                                                                     |
u[7]: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X---------------------------------------------------------------------------------
        """, use_unicode_characters=False)
Ejemplo n.º 26
0
def test_redundant_lookup():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='addr') as addr:
                with qp.qalloc(len=8, name='out') as out:
                    with qp.qalloc(name='cnt') as cnt:
                        out ^= qp.LookupTable(
                            [3] * 16)[addr] & qp.controlled_by(cnt)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
addr[0]: ---alloc-------------------------------------------release---
            |                                               |
addr[1]: ---alloc-------------------------------------------release---
            |                                               |
addr[2]: ---alloc-------------------------------------------release---
            |                                               |
addr[3]: ---alloc-------------------------------------------release---

cnt: -----------------------alloc---@---release-----------------------
                                    |
out[0]: ------------alloc-----------X-------------release-------------
                    |               |             |
out[1]: ------------alloc-----------X-------------release-------------
                    |                             |
out[2]: ------------alloc-------------------------release-------------
                    |                             |
out[3]: ------------alloc-------------------------release-------------
                    |                             |
out[4]: ------------alloc-------------------------release-------------
                    |                             |
out[5]: ------------alloc-------------------------release-------------
                    |                             |
out[6]: ------------alloc-------------------------release-------------
                    |                             |
out[7]: ------------alloc-------------------------release-------------
        """,
                                    use_unicode_characters=False)
Ejemplo n.º 27
0
def test_del_lookup():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='addr') as addr:
                with qp.qalloc(name='cnt'):
                    with qp.hold(qp.LookupTable(range(1, 17))[addr],
                                 name='out'):
                        circuit[:] = []

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_lookup_prefix: ---------------------------------------alloc---X---X-----------@---@---------------@-----------------@-------------------X-----------@---@-------@---------------------@-------------------Mxc-------cxM---release--------------------------------------------------------------------------------------------------------------------
                                                               |               |   |               |                 |                               |   |       |                     |
_lookup_prefix_1: ---------------------------------------------|-------alloc---X---X---@---@---@---X---@---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---@---@---Mxc---|---cxM---release----------------------------------------------------------------------------------------------------------------------------------------------
                                                               |               |       |   |   |       |   |         |                               |       |       |   |   |         |
addr[0]: ----------------------@-------------------------------|---------------|-------|---|---|-------|---|---------|-------------------------------|-------|-------|---|---|---------|-----------------------------------------------------------------------------------------------Z----------------------------------------------------release---
                               |                               |               |       |   |   |       |   |         |                               |       |       |   |   |         |                                                                                               |                                                    |
addr[1]: ----------------------|-------@-------@---------------|---------------|-------|---|---|-------|---|---------|-------------------------------|-------|-------|---|---|---------|-------------------------------------------------------Z-------------------Z-------------------|----------------------------------------------------release---
                               |       |       |               |               |       |   |   |       |   |         |                               |       |       |   |   |         |                                                       |                   |                   |                                                    |
addr[2]: ----------------------|-------|-------|---------------|---------------@-------|---|---|-------|---|---------Z-------------------------------@-------|-------|---|---|---------Z-------------------------------------------------------|-------------------|-------------------|----------------------------------------------------release---
                               |       |       |               |                       |   |   |       |   |                                                 |       |   |   |                                                                 |                   |                   |                                                    |
addr[3]: ----------------------|-------|-------|---------------@-----------------------|---|---|-------|---|-------------------------------------------------|-------|---|---|-----------------------------------Z-----------------------------|-------------------|-------------------|----------------------------------------------------release---
                               |       |       |                                       |   |   |       |   |                                                 |       |   |   |                                                                 |                   |                   |
cnt: --------------------------|-------|-------|---------------------------------------|---|---|-------|---|-------------------------------------------------|-------|---|---|-----------------------------------------------------------------|-------------------|-------------------|------------------------------------------release-------------
                               |       |       |                                       |   |   |       |   |                                                 |       |   |   |                                                                 |                   |                   |
out[0]: -------------Mxc---X---@---X---@---X---|---------------------------------------Z---|---|-------|---|-------------------------------------------------|-------Z---|---|-----------------------------------------------------------------|---------X---------@---------X---------@---------Mxc--------cxM---cxM---release-----------------------
                     |         |   |   |   |   |                                           |   |       |   |                                                 |           |   |                                                                 |         |                   |                                    |     |
out[1]: -------------Mxc-------X---@---|---|---@---X---------------------------------------Z---|-------|---|-------------------------------------------------|-----------Z---|-------------------------------------------------------X---------@---------|-------------------@---Mxc-------cxM--------------------cxM---release-----------------------
                     |                 |   |   |   |                                           |       |   |                                                 |               |                                                       |                   |                                                        |     |
out[2]: -------------Mxc---------------X---@---|---|-------------------------------------------|-------Z---|-------------------------------------------------Z---------------|-------------------------------------------------------|-------------------@---Mxc-------cxM----------------------------------------cxM---release-----------------------
                     |                         |   |                                           |           |                                                                 |                                                       |                                                                            |     |
out[3]: -------------Mxc-----------------------X---@-------------------------------------------Z-----------Z-----------------------------------------------------------------Z-------------------------------------------------------@---Mxc-------cxM------------------------------------------------------------cxM---release-----------------------
                     |                                                                                                                                                                                                                                                                                            |     |
out[4]: -------------Mxc------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------cxM---release-----------------------

global phase:                                                                                                                                                                                                                                                                                          pi
        """,
                                    use_unicode_characters=False)