Skip to content

Commit

Permalink
Enable decomposition for IntEffect too
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan committed Oct 6, 2023
1 parent 1ab6a7d commit df6fa31
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
29 changes: 20 additions & 9 deletions qualtran/bloqs/basic_gates/z_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,26 @@ def signature(self) -> Signature:
side = Side.RIGHT if self.state else Side.LEFT
return Signature([Register('val', bitsize=self.bitsize, side=side)])

def build_composite_bloq(self, bb: 'BloqBuilder') -> Dict[str, 'SoquetT']:
states = [ZeroState(), OneState()]
xs = []
for bit in ints_to_bits(np.array([self.val]), w=self.bitsize)[0]:
x = bb.add(states[bit])
xs.append(x)
xs = np.array(xs)

return {'val': bb.join(xs)}
def build_composite_bloq(self, bb: 'BloqBuilder', **val) -> Dict[str, 'SoquetT']:
bits = ints_to_bits(np.array([self.val]), w=self.bitsize)[0]

if self.state:
assert not val
states = [ZeroState(), OneState()]
xs = []
for bit in bits:
x = bb.add(states[bit])
xs.append(x)
xs = np.array(xs)

return {'val': bb.join(xs)}

val = val['val']
xs = bb.split(val)
effects = [ZeroEffect(), OneEffect()]
for i, bit in enumerate(bits):
bb.add(effects[bit], q=xs[i])
return {}

def add_my_tensors(
self,
Expand Down
4 changes: 4 additions & 0 deletions qualtran/bloqs/basic_gates/z_basis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import numpy as np
import pytest

import qualtran.testing as qlt_testing
from qualtran import BloqBuilder
from qualtran.bloqs.basic_gates import (
IntEffect,
Expand Down Expand Up @@ -147,6 +148,9 @@ def test_int_effect():
with pytest.raises(AssertionError):
k.call_classically(val=245)

qlt_testing.assert_valid_bloq_decomposition(k)
np.testing.assert_allclose(k.tensor_contract(), k.decompose_bloq().tensor_contract())


def test_to_cirq():
bb = BloqBuilder()
Expand Down

0 comments on commit df6fa31

Please sign in to comment.