Skip to content

Commit

Permalink
Merge branch 'main' into be
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesyuan314 committed Jun 28, 2024
2 parents 110b29b + 79a880b commit ea689ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 8 additions & 2 deletions qualtran/bloqs/bookkeeping/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CompositeBloq,
DecomposeTypeError,
QDType,
QFxp,
Register,
Side,
Signature,
Expand Down Expand Up @@ -100,8 +101,13 @@ def add_my_tensors(
)

def on_classical_vals(self, reg: int) -> Dict[str, 'ClassicalValT']:
# TODO: Actually cast the values https://github.com/quantumlib/Qualtran/issues/734
return {'reg': reg}
if isinstance(self.out_dtype, QFxp):
res = reg
elif isinstance(self.inp_dtype, QFxp):
res = int(reg)
else:
res = self.out_dtype.from_bits(self.inp_dtype.to_bits(reg))
return {'reg': res}

def as_cirq_op(self, qubit_manager, reg: 'CirqQuregT') -> Tuple[None, Dict[str, 'CirqQuregT']]:
return None, {'reg': reg}
Expand Down
13 changes: 12 additions & 1 deletion qualtran/bloqs/bookkeeping/cast_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
import subprocess

from qualtran import QFxp, QInt
from qualtran import QFxp, QInt, QUInt
from qualtran.bloqs.bookkeeping import Cast
from qualtran.bloqs.bookkeeping.cast import _cast
from qualtran.bloqs.for_testing import TestCastToFrom
Expand All @@ -40,6 +40,17 @@ def test_cast_classical_sim():
assert a == 7
assert b == 9

c = Cast(QFxp(8, 8), QUInt(8))
assert c.call_classically(reg=1.2) == (1,) # type: ignore


def test_cast_unsiged_signed():
c = Cast(QUInt(5), QInt(5))
assert c.call_classically(reg=31) == (-1,)

c = Cast(QInt(5), QUInt(5))
assert c.call_classically(reg=-1) == (31,)


def test_no_circular_import():
subprocess.check_call(['python', '-c', 'from qualtran.bloqs.bookkeeping import cast'])

0 comments on commit ea689ca

Please sign in to comment.