Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement classical action of Cast bloq #1093

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'])
Loading