def test_iadd_isub(): q = qp.Quint(qp.NamedQureg('test', 10)) with pytest.raises(TypeError): q += None with qp.RandomSim(measure_bias=0.5): with qp.capture() as out: q += 5 assert qp.ccz_count(out) == 18 with qp.RandomSim(measure_bias=0.5): with qp.capture() as out: q += 4 assert qp.ccz_count(out) == 14 with qp.RandomSim(measure_bias=0.5): with qp.capture() as out: q -= 3 assert qp.ccz_count(out) == 18 q2 = qp.Quint(qp.NamedQureg('test2', 5)) with qp.RandomSim(measure_bias=0.5): with qp.capture() as out: q += q2 assert qp.ccz_count(out) == 18 # Classes can specify custom behavior via __riadd__. class Riadd: def __riadd__(self, other): qp.phase_flip() return other with qp.capture() as out: q += Riadd() assert out == [('phase_flip', qp.QubitIntersection.ALWAYS)]
def test_range_qureg_init(): eq = cirq.testing.EqualsTester() a = qp.NamedQureg('a', 5) b = qp.NamedQureg('b', 5) eq.add_equality_group(a[:2]) eq.add_equality_group(a[:3]) eq.add_equality_group(b[:3])
def test_named_qureg_init(): eq = cirq.testing.EqualsTester() q1 = qp.NamedQureg('test', 10) q2 = qp.NamedQureg('test', 10) assert str(q1) == str(q2) == 'test' eq.add_equality_group(qp.NamedQureg('', 5)) eq.add_equality_group(qp.NamedQureg('', 6)) eq.add_equality_group(q1, q2) eq.add_equality_group(qp.NamedQureg('q', 2))
def test_len_getitem(): h = 'test' q = qp.Quint(qp.NamedQureg(h, 10)) assert len(q) == 10 with pytest.raises(IndexError): _ = q[-100] assert q[0] == qp.Qubit(h, 0) assert q[-1] == qp.Qubit(h, 9) assert q[2:4].qureg == qp.Quint(qp.RangeQureg( qp.NamedQureg(h, 10), range(2, 4))).qureg
def test_named_qureg_get_item_len(): h = 'a' q = qp.NamedQureg(h, 5) assert q[0] == qp.Qubit(h, 0) assert len(q) == 5 assert q[:] == q assert q[2:4] == qp.RangeQureg(q, range(2, 4))
def qfree( target: Union[qp.Qubit, qp.Qureg, qp.Quint], equivalent_expression: 'Union[None, bool, int, qp.RValue[Any]]' = None, dirty: bool = False): """Deallocates quantum objects. Args: target: The quantum object to free. equivalent_expression: An entangled expression with the same computational basis value as the quantum object. Used to uncompute the quantum object before freeing it, to avoid revealing information. dirty: Indicates that the quantum object is not expected to be zero'd. """ if equivalent_expression is not None: qp.rval(equivalent_expression).clear_storage_location( target, qp.QubitIntersection.ALWAYS) if isinstance(target, qp.Qubit): assert target.index is None, "Can't deallocate individual qubits from within a register." reg = qp.NamedQureg(name=target.name, length=1) elif isinstance(target, qp.Qureg): reg = target elif isinstance(target, qp.Quint): reg = target.qureg elif isinstance(target, qp.QuintMod): reg = target.qureg else: raise NotImplementedError() if len(reg): sink.global_sink.do_release(qp.ReleaseQuregOperation(reg, dirty=dirty))
def test_quint_borrowed(): @qp.semi_quantum def f(x: qp.Quint.Borrowed): return x q = qp.Quint(qp.NamedQureg('a', 10)) assert f(q) is q with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: v = f(2) assert isinstance(v, qp.Quint) cirq.testing.assert_has_diagram(circuit, """ _f_x[0]: -------alloc-------Mxc--------cxM---release--- | | | | _f_x[1]: -------alloc---X---Mxc--------cxM---release--- global phase: pi """, use_unicode_characters=False) with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: v = f(True) assert isinstance(v, qp.Quint) cirq.testing.assert_has_diagram(circuit, """ _f_x: ----------alloc---X---Mxc--------cxM---release--- global phase: pi """, use_unicode_characters=False) with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: rval = qp.LookupTable([1, 2, 3])[q] v = f(rval) assert isinstance(v, qp.Quint) cirq.testing.assert_has_diagram(circuit, """ _f_x[0]: ----------alloc-------------------------------------X------------------------------------------X-----------------------------Mxc---X---@---X-------------------Z-------------------------------------X---------@---------Mxc--------cxM---cxM---release--- | | | | | | | | | | | _f_x[1]: ----------alloc-------------------------------------|--------X---------------------------------X-----------------------------Mxc-------X---@-------------------|---Z---------------------------------@---Mxc---|---cxM--------------------cxM---release--- | | | | | | | _lookup_prefix: -----------alloc---X---X---alloc---@X---@X---@---@X---@---Mxc---@---cxM---release---X---@---Mxc-------cxM---release-------------|-------alloc---X---X---@---@---X---Mxc-------cxM---release-------------|------------------------------------------ | | | | | | a[0]: -----------------------------|---------------@----------------------------Z---------------------------------------------------------------@---------------|-------------------------------------------------------Z------------------------------------------ | | a[1]: -----------------------------@------------------------------------------------------------------------------Z---------------------------------------------@-------------------------Z------------------------------------------------------------------------ global phase: pi """, use_unicode_characters=False) with pytest.raises(TypeError, match='quantum integer expression'): _ = f('test')
def test_range_qureg_getitem_len(): h = 'a' a = qp.NamedQureg(h, 5) r = qp.RangeQureg(a, range(1, 3)) assert r[0] == qp.Qubit(h, 1) assert r[1] == qp.Qubit(h, 2) assert r[-1] == qp.Qubit(h, 2) with pytest.raises(IndexError): _ = r[2]
def test_qubit(): @qp.semi_quantum def f(x: qp.Qubit): return x q = qp.Qubit('a', 10) assert f(q) is q with pytest.raises(TypeError, match='Expected a qp.Qubit'): _ = f(2) with pytest.raises(TypeError, match='Expected a qp.Qubit'): _ = f('a') with pytest.raises(TypeError, match='Expected a qp.Qubit'): _ = f(qp.Quint(qp.NamedQureg('a', 10))) with pytest.raises(TypeError, match='Expected a qp.Qubit'): _ = f(qp.BoolRValue(True))
def test_multiple(): @qp.semi_quantum def add(target: qp.Quint, offset: qp.Quint.Borrowed, *, control: qp.Qubit.Control = False): assert isinstance(target, qp.Quint) assert isinstance(offset, qp.Quint) assert isinstance(control, qp.QubitIntersection) target += offset & qp.controlled_by(control) a = qp.Quint(qp.NamedQureg('a', 5)) with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: add(a, 10, control=True) assert len(circuit) >= 5
def do_allocate(self, args: 'qp.AllocArgs') -> 'qp.Qureg': if args.qureg_name is None: name = f'_anon_{self._anon_alloc_counter}' self._anon_alloc_counter += 1 else: name = args.qureg_name if name in self._int_state: k = 1 while True: candidate = f'{name}_{k}' if candidate not in self._int_state: break k += 1 name = candidate result = qp.NamedQureg(name=name, length=args.qureg_length) self._int_state[result.name] = qp.IntBuf.raw( val=random.randint(0, (1 << args.qureg_length) - 1) if args.x_basis else 0, length=args.qureg_length) return result
def test_set_item_blocks(): q = qp.Quint(qp.NamedQureg('test', 10)) with pytest.raises(NotImplementedError): q[2] = qp.Qubit()
def test_named_qureg_repr(): cirq.testing.assert_equivalent_repr( qp.NamedQureg('a', 3), setup_code='import quantumpseudocode as qp')
def qureg(self): if self.index is None: return qp.NamedQureg(self.name, length=1) return qp.RawQureg([qp.Qubit(self.name, self.index)])
def do_allocate(self, args: 'qp.AllocArgs') -> 'qp.Qureg': result = qp.NamedQureg(args.qureg_name or '', length=args.qureg_length) self.did_allocate(args, result) return result
def test_init(): q1 = qp.Quint(qp.NamedQureg('test', 10)) q2 = qp.Quint(qp.NamedQureg('test', 10)) assert q1.qureg == q2.qureg assert str(q1) == str(q2) == 'test'
def test_ixor(): q = qp.Quint(qp.NamedQureg('test', 10)) with pytest.raises(TypeError): q ^= None with qp.LogCirqCircuit() as circuit: q ^= 5 cirq.testing.assert_has_diagram(circuit, """ test[0]: ---X--- | test[2]: ---X--- """, use_unicode_characters=False) q2 = qp.Quint(qp.NamedQureg('test2', 5)) with qp.LogCirqCircuit() as circuit: q ^= q2 cirq.testing.assert_has_diagram(circuit, """ test2[0]: ---@------------------- | test2[1]: ---|---@--------------- | | test2[2]: ---|---|---@----------- | | | test2[3]: ---|---|---|---@------- | | | | test2[4]: ---|---|---|---|---@--- | | | | | test[0]: ----X---|---|---|---|--- | | | | test[1]: --------X---|---|---|--- | | | test[2]: ------------X---|---|--- | | test[3]: ----------------X---|--- | test[4]: --------------------X--- """, use_unicode_characters=False) q3 = qp.Quint(qp.NamedQureg('test3', 5)) c = qp.Qubit('c') with qp.LogCirqCircuit() as circuit: q ^= q3 & qp.controlled_by(c) cirq.testing.assert_has_diagram(circuit, """ c: ----------@---@---@---@---@--- | | | | | test3[0]: ---@---|---|---|---|--- | | | | | test3[1]: ---|---@---|---|---|--- | | | | | test3[2]: ---|---|---@---|---|--- | | | | | test3[3]: ---|---|---|---@---|--- | | | | | test3[4]: ---|---|---|---|---@--- | | | | | test[0]: ----X---|---|---|---|--- | | | | test[1]: --------X---|---|---|--- | | | test[2]: ------------X---|---|--- | | test[3]: ----------------X---|--- | test[4]: --------------------X--- """, use_unicode_characters=False) # Classes can specify custom behavior via __rixor__. class Rixor: def __rixor__(self, other): qp.phase_flip() return other with qp.capture() as out: q ^= Rixor() assert out == [('phase_flip', qp.QubitIntersection.ALWAYS)]
def test_qubit_control(): @qp.semi_quantum def f(x: qp.Qubit.Control): return x q = qp.Qubit('a', 10) q2 = qp.Qubit('b', 8) # Note: The lack of capture context means we are implicitly asserting the following invokations perform no # quantum operations such as allocating a qubit. # Definitely false. assert f(False) == qp.QubitIntersection.NEVER assert f(qp.QubitIntersection.NEVER) == qp.QubitIntersection.NEVER # Definitely true. assert f(qp.QubitIntersection.ALWAYS) == qp.QubitIntersection.ALWAYS assert f(None) == qp.QubitIntersection.ALWAYS assert f(True) == qp.QubitIntersection.ALWAYS # Single qubit. assert f(q) == qp.QubitIntersection((q, )) assert f(qp.QubitIntersection((q, ))) == qp.QubitIntersection((q, )) # Multi qubit intersection. with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: v = f(q & q2) assert isinstance(v, qp.QubitIntersection) del v cirq.testing.assert_has_diagram(circuit, """ _f_x: ----alloc---X---Mxc-------cxM---release--- | a[10]: -----------@---------@------------------- | | b[8]: ------------@---------Z------------------- """, use_unicode_characters=False) # Arbitrary expression with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: rval = qp.Quint(qp.NamedQureg('a', 2)) > qp.Quint( qp.NamedQureg('b', 2)) v = f(rval) assert isinstance(v, qp.QubitIntersection) q = v.qubits[0] assert q.name == '_f_x' del q del v cirq.testing.assert_has_diagram(circuit, """ _do_if_less_than_or_equal: -----------alloc---@---X---@-------------------------------@---X---@---Mxc---cxM---release---------alloc---@---X---@-------------------------------@---X---@---Mxc---cxM---release------------------- | | | | | | | | | | | | _f_x: ------------------------alloc-----------|---|---|---------------X---------------|---|---|-------------------------Mxc-----------|---|---|-------------------------------|---|---|-------------------------cxM---release--- | | | | | | | | | | | | | a[0]: ----------------------------------------|---@---X---@---X---@---|---@---X---@---X---@---|---------------------------------------|---@---X---@---X---@-------@---X---@---X---@---|----------------------------------------- | | | | | | | | | | | | | | | | | | | | | a[1]: ----------------------------------------|-------|---|---@---X---@---X---@---|---|-------|---------------------------------------|-------|---|---@---X---Z---X---@---|---|-------|----------------------------------------- | | | | | | | | | | | | | | | | b[0]: ----------------------------------------X-------@---|-------|-------|-------|---@-------X---------------------------------------X-------@---|-------|-------|-------|---@-------X----------------------------------------- | | | | | | | | b[1]: ----------------------------------------------------X-------@-------@-------X---------------------------------------------------------------X-------@-------@-------X----------------------------------------------------- """, use_unicode_characters=False) with pytest.raises(TypeError, match='quantum control expression'): _ = f('test') with pytest.raises(TypeError, match='quantum control expression'): _ = f(qp.Quint(qp.NamedQureg('a', 10))) with pytest.raises(TypeError, match='quantum control expression'): _ = f(qp.Quint(qp.NamedQureg('a', 10)))
def test_range_qureg_repr(): h = 'a' a = qp.NamedQureg(h, 5) r = qp.RangeQureg(a, range(1, 3)) cirq.testing.assert_equivalent_repr( r, setup_code='import quantumpseudocode as qp')
def test_mul_rmul(): q = qp.Quint(qp.NamedQureg('test', 10)) assert q * 5 == 5 * q == qp.ScaledIntRValue(q, 5)
def test_qubit_borrowed(): @qp.semi_quantum def f(x: qp.Qubit.Borrowed): return x q = qp.Qubit('a', 10) assert f(q) is q with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: v = f(True) assert isinstance(v, qp.Qubit) del v cirq.testing.assert_has_diagram(circuit, """ _f_x: ----------alloc---X---Mxc--------cxM---release--- global phase: pi """, use_unicode_characters=False) with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: v = f(0) assert isinstance(v, qp.Qubit) del v cirq.testing.assert_has_diagram(circuit, """ _f_x: ---alloc---Mxc---cxM---release--- """, use_unicode_characters=False) with qp.RandomSim(measure_bias=1): with qp.LogCirqCircuit() as circuit: rval = qp.Quint(qp.NamedQureg('a', 3)) > qp.Quint( qp.NamedQureg('b', 3)) v = f(rval) assert isinstance(v, qp.Qubit) del v cirq.testing.assert_has_diagram(circuit, """ _do_if_less_than_or_equal: -----------alloc---@---X---@-------------------------------------------------------@---X---@---Mxc---cxM---release---------alloc---@---X---@-------------------------------------------------------@---X---@---Mxc---cxM---release------------------- | | | | | | | | | | | | _f_x: ------------------------alloc-----------|---|---|---------------------------X---------------------------|---|---|-------------------------Mxc-----------|---|---|-------------------------------------------------------|---|---|-------------------------cxM---release--- | | | | | | | | | | | | | a[0]: ----------------------------------------|---@---X---@---X---@---------------|---------------@---X---@---X---@---|---------------------------------------|---@---X---@---X---@-------------------------------@---X---@---X---@---|----------------------------------------- | | | | | | | | | | | | | | | | | | | | | a[1]: ----------------------------------------|-------|---|---@---X---@---X---@---|---@---X---@---X---@---|---|-------|---------------------------------------|-------|---|---@---X---@---X---@-------@---X---@---X---@---|---|-------|----------------------------------------- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | a[2]: ----------------------------------------|-------|---|-------|---|---@---X---@---X---@---|---|-------|---|-------|---------------------------------------|-------|---|-------|---|---@---X---Z---X---@---|---|-------|---|-------|----------------------------------------- | | | | | | | | | | | | | | | | | | | | | | | | b[0]: ----------------------------------------X-------@---|-------|---|-------|-------|-------|---|-------|---@-------X---------------------------------------X-------@---|-------|---|-------|-------|-------|---|-------|---@-------X----------------------------------------- | | | | | | | | | | | | | | | | b[1]: ----------------------------------------------------X-------@---|-------|-------|-------|---@-------X---------------------------------------------------------------X-------@---|-------|-------|-------|---@-------X----------------------------------------------------- | | | | | | | | b[2]: ----------------------------------------------------------------X-------@-------@-------X---------------------------------------------------------------------------------------X-------@-------@-------X----------------------------------------------------------------- """, use_unicode_characters=False) with pytest.raises(TypeError, match='quantum boolean expression'): _ = f('test')