From 0bf245af14f5617e09f0c488a7ff88c6eb76eb38 Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Tue, 18 Jun 2024 17:26:03 -0700 Subject: [PATCH] rename _2 -> --- qualtran/_infra/controlled_test.py | 4 ++-- qualtran/bloqs/bookkeeping/cast_test.py | 4 ++-- qualtran/bloqs/bookkeeping/partition_test.py | 4 ++-- qualtran/simulation/tensor/__init__.py | 2 +- qualtran/simulation/tensor/_dense.py | 4 ++-- qualtran/simulation/tensor/_quimb.py | 11 ++++++++++- qualtran/simulation/tensor/_quimb_test.py | 4 ++-- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/qualtran/_infra/controlled_test.py b/qualtran/_infra/controlled_test.py index eac2c71ef9..19f52836b5 100644 --- a/qualtran/_infra/controlled_test.py +++ b/qualtran/_infra/controlled_test.py @@ -52,7 +52,7 @@ from qualtran.cirq_interop.testing import GateHelper from qualtran.drawing import get_musical_score_data from qualtran.drawing.musical_score import Circle, SoqData, TextBox -from qualtran.simulation.tensor import cbloq_to_quimb_2, get_right_and_left_inds +from qualtran.simulation.tensor import cbloq_to_quimb, get_right_and_left_inds if TYPE_CHECKING: from qualtran import SoquetT @@ -371,7 +371,7 @@ def test_controlled_tensor_without_decompose(): cbloq = Controlled(bloq, ctrl_spec) cgate = cirq.ControlledGate(cirq.CSWAP, control_values=ctrl_spec.to_cirq_cv()) - tn = cbloq_to_quimb_2(cbloq.as_composite_bloq()) + tn = cbloq_to_quimb(cbloq.as_composite_bloq()) # pylint: disable=unbalanced-tuple-unpacking right, left = get_right_and_left_inds(tn, cbloq.signature) # pylint: enable=unbalanced-tuple-unpacking diff --git a/qualtran/bloqs/bookkeeping/cast_test.py b/qualtran/bloqs/bookkeeping/cast_test.py index 04630a6285..94885e39db 100644 --- a/qualtran/bloqs/bookkeeping/cast_test.py +++ b/qualtran/bloqs/bookkeeping/cast_test.py @@ -17,7 +17,7 @@ from qualtran.bloqs.bookkeeping import Cast from qualtran.bloqs.bookkeeping.cast import _cast from qualtran.bloqs.for_testing import TestCastToFrom -from qualtran.simulation.tensor import cbloq_to_quimb_2 +from qualtran.simulation.tensor import cbloq_to_quimb def test_cast(bloq_autotester): @@ -26,7 +26,7 @@ def test_cast(bloq_autotester): def test_cast_tensor_contraction(): bloq = TestCastToFrom() - tn = cbloq_to_quimb_2(bloq.as_composite_bloq().flatten()) + tn = cbloq_to_quimb(bloq.as_composite_bloq().flatten()) assert tn.shape == (2,) * 4 * 4 diff --git a/qualtran/bloqs/bookkeeping/partition_test.py b/qualtran/bloqs/bookkeeping/partition_test.py index 4b9a5121d9..3c97cc465d 100644 --- a/qualtran/bloqs/bookkeeping/partition_test.py +++ b/qualtran/bloqs/bookkeeping/partition_test.py @@ -25,7 +25,7 @@ from qualtran.bloqs.bookkeeping import Partition from qualtran.bloqs.bookkeeping.partition import _partition from qualtran.bloqs.for_testing import TestMultiRegister -from qualtran.simulation.tensor import bloq_to_dense, cbloq_to_quimb_2 +from qualtran.simulation.tensor import bloq_to_dense, cbloq_to_quimb from qualtran.testing import assert_valid_bloq_decomposition @@ -66,7 +66,7 @@ def test_partition_wrapper(): def test_partition_wrapper_tensor_contract(): bloq = TestPartition(test_bloq=TestMultiRegister()) - tn = cbloq_to_quimb_2(bloq.as_composite_bloq().flatten()) + tn = cbloq_to_quimb(bloq.as_composite_bloq().flatten()) assert tn.shape == (2,) * (2 * int(np.log2(4096))) tn = tn.rank_simplify() diff --git a/qualtran/simulation/tensor/__init__.py b/qualtran/simulation/tensor/__init__.py index 6538d1913f..4acc6c777d 100644 --- a/qualtran/simulation/tensor/__init__.py +++ b/qualtran/simulation/tensor/__init__.py @@ -14,7 +14,7 @@ from ._dense import bloq_to_dense, get_right_and_left_inds, quimb_to_dense -from ._quimb import cbloq_to_quimb_2, initialize_from_zero +from ._quimb import cbloq_to_quimb, initialize_from_zero from ._tensor_data_manipulation import ( active_space_for_ctrl_spec, eye_tensor_for_signature, diff --git a/qualtran/simulation/tensor/_dense.py b/qualtran/simulation/tensor/_dense.py index e27f4f9d36..c56f7e8b4e 100644 --- a/qualtran/simulation/tensor/_dense.py +++ b/qualtran/simulation/tensor/_dense.py @@ -19,7 +19,7 @@ from qualtran import Bloq, Connection, ConnectionT, LeftDangle, RightDangle, Signature, Soquet -from ._quimb import cbloq_to_quimb_2 +from ._quimb import cbloq_to_quimb if TYPE_CHECKING: import quimb.tensor as qtn @@ -143,5 +143,5 @@ def bloq_to_dense(bloq: Bloq) -> NDArray: """ logging.info("bloq_to_dense() on %s", bloq) flat_cbloq = bloq.as_composite_bloq().flatten() - tn = cbloq_to_quimb_2(flat_cbloq) + tn = cbloq_to_quimb(flat_cbloq) return quimb_to_dense(tn, bloq.signature) diff --git a/qualtran/simulation/tensor/_quimb.py b/qualtran/simulation/tensor/_quimb.py index 63fcf94000..70cfdd901b 100644 --- a/qualtran/simulation/tensor/_quimb.py +++ b/qualtran/simulation/tensor/_quimb.py @@ -33,7 +33,16 @@ logger = logging.getLogger(__name__) -def cbloq_to_quimb_2(cbloq: CompositeBloq) -> qtn.TensorNetwork: +def cbloq_to_quimb(cbloq: CompositeBloq) -> qtn.TensorNetwork: + """Convert a composite bloq into a tensor network. + + This function will call `Bloq.my_tensors` on each subbloq in the composite bloq to add + tensors to a quimb tensor network. This method has no default fallback, so you likely want to + call `bloq.as_composite_bloq().flatten()` to decompose-and-flatten all bloqs down to their + smallest form first. The small bloqs that result from a flattening 1) likely already have + their `my_tensors` method implemented; and 2) can enable a more efficient tensor contraction + path. + """ tn = qtn.TensorNetwork([]) logging.info( diff --git a/qualtran/simulation/tensor/_quimb_test.py b/qualtran/simulation/tensor/_quimb_test.py index 023ab3d0d9..cde0e06e0e 100644 --- a/qualtran/simulation/tensor/_quimb_test.py +++ b/qualtran/simulation/tensor/_quimb_test.py @@ -21,7 +21,7 @@ from qualtran import Bloq, BloqBuilder, Connection, ConnectionT, DanglingT, QAny, Signature from qualtran.bloqs.bookkeeping import Join, Split -from qualtran.simulation.tensor import cbloq_to_quimb_2 +from qualtran.simulation.tensor import cbloq_to_quimb @frozen @@ -47,7 +47,7 @@ def test_cbloq_to_quimb(): x = bb.add(TensorAdderSimple(), x=x) cbloq = bb.finalize(x=x) - tn = cbloq_to_quimb_2(cbloq) + tn = cbloq_to_quimb(cbloq) assert len(tn.tensors) == 4 for outer_ind in tn.outer_inds(): cxn, j = outer_ind