Exemple #1
0
    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
Exemple #2
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)
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)
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)
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)
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)
Exemple #7
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
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)
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)
Exemple #10
0
def do_plus_const_mod(*,
                      control: qp.Qubit.Control = True,
                      lvalue: qp.Quint,
                      offset: int,
                      modulus: int,
                      forward: bool = True):
    assert isinstance(control,
                      qp.QubitIntersection) and len(control.qubits) <= 1
    assert isinstance(lvalue, qp.Quint)
    assert isinstance(offset, int)
    assert isinstance(modulus, int)
    assert modulus > 0
    n = (modulus - 1).bit_length()
    assert len(lvalue) >= n
    if not forward:
        offset *= -1
    offset %= modulus

    if not modulus & (modulus - 1):
        lvalue += offset & qp.controlled_by(control)
        return

    with qp.qalloc(name='mod_cmp') as q:
        q.init(lvalue >= modulus - offset, controls=control)
        lvalue += offset & qp.controlled_by(control)
        lvalue -= modulus & qp.controlled_by(q & control)
        q.clear(lvalue < offset, controls=control)
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
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_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
Exemple #14
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)
Exemple #15
0
    def __enter__(self) -> 'qp.Qureg':
        if len(self.base) >= self.min_len:
            return self.wrapper(self.base)

        assert self.padded is None
        sub_name = str(self.base) if isinstance(self.base,
                                                qp.NamedQureg) else ''
        self.padded = qp.qalloc(name='{}_pad'.format(sub_name),
                                len=self.min_len - len(self.base))
        return self.wrapper(qp.RawQureg(list(self.base) + list(self.padded)))
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
Exemple #17
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_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)
Exemple #19
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)
Exemple #20
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)
Exemple #21
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)
Exemple #22
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)
Exemple #23
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
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)
Exemple #25
0
def do_plus_mod(control: 'qp.Qubit.Control' = True,
                *,
                lvalue: qp.Quint,
                offset: qp.Quint.Borrowed,
                modulus: int,
                forward: bool = True):
    assert isinstance(lvalue, qp.Quint)
    assert modulus > 0
    n = (modulus - 1).bit_length()
    assert len(offset) <= len(lvalue)
    assert len(lvalue) >= n

    if not modulus & (modulus - 1):
        if forward:
            lvalue[:n] += offset & qp.controlled_by(control)
        else:
            lvalue[:n] -= offset & qp.controlled_by(control)
        return

    with offset.hold_padded_to(n) as offset:
        with qp.qalloc(name='mod_cmp') as q:
            if forward:
                offset ^= -1
                offset += modulus + 1
                q.init(lvalue >= offset, controls=control)
                offset -= modulus + 1
                offset ^= -1

                lvalue += offset & qp.controlled_by(control)
                lvalue -= modulus & qp.controlled_by(q & control)
                q.clear(lvalue < offset, controls=control)
            else:
                q.init(lvalue < offset, controls=control)
                lvalue += modulus & qp.controlled_by(q & control)
                lvalue -= offset & qp.controlled_by(control)

                offset ^= -1
                offset += modulus + 1
                q.clear(lvalue >= offset, controls=control)
                offset -= modulus + 1
                offset ^= -1
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)
 def alloc_storage_location(self, name: Optional[str] = None):
     return qp.qalloc(len=self.table.output_len(), name=name)
Exemple #28
0
 def alloc_storage_location(self, name: Optional[str] = None):
     return qp.qalloc(len=len(self.coherent) + self.constant.bit_length(), name=name)
Exemple #29
0
 def alloc_storage_location(self, name: Optional[str] = None):
     return qp.qalloc(name=name)
 def alloc_storage_location(self, name: Optional[str] = None):
     return qp.qalloc(len=1 << len(self.binary), name=name)