Skip to content
Open
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
12 changes: 4 additions & 8 deletions qualtran/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
CompositeBloq,
BloqBuilder,
DidNotFlattenAnythingError,
Soquet,
SoquetT,
ConnectionT,
QVar,
QVarT,
)

from ._infra.data_types import (
Expand Down Expand Up @@ -84,14 +87,7 @@

# Internal imports: none
# External imports: none
from ._infra.quantum_graph import (
BloqInstance,
Connection,
DanglingT,
LeftDangle,
RightDangle,
Soquet,
)
from ._infra.quantum_graph import BloqInstance, Connection, DanglingT, LeftDangle, RightDangle

from ._infra.gate_with_registers import GateWithRegisters

Expand Down
20 changes: 14 additions & 6 deletions qualtran/_infra/adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@

from attrs import frozen

from .composite_bloq import _binst_to_cxns, _cxns_to_soq_dict, _map_soqs, _reg_to_soq, BloqBuilder
from .composite_bloq import (
_binst_to_cxns,
_cxns_to_soq_dict,
_map_soqs,
_reg_to_soq,
_SoquetT,
BloqBuilder,
QVarT,
)
from .gate_with_registers import GateWithRegisters
from .quantum_graph import LeftDangle, RightDangle
from .registers import Signature

if TYPE_CHECKING:
import cirq

from qualtran import Bloq, CompositeBloq, Register, Signature, SoquetT
from qualtran import Bloq, CompositeBloq, Register, Signature
from qualtran.drawing import WireSymbol
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator


def _adjoint_final_soqs(cbloq: 'CompositeBloq', new_signature: Signature) -> Dict[str, 'SoquetT']:
def _adjoint_final_soqs(cbloq: 'CompositeBloq', new_signature: Signature) -> Dict[str, '_SoquetT']:
"""`CompositeBloq.final_soqs()` but backwards."""
if LeftDangle not in cbloq._binst_graph:
return {}
Expand All @@ -57,15 +65,15 @@ def _adjoint_cbloq(cbloq: 'CompositeBloq') -> 'CompositeBloq':
# First, we reverse the registers to initialize the BloqBuilder.
old_signature = cbloq.signature
new_signature = cbloq.signature.adjoint()
old_i_soqs = [_reg_to_soq(RightDangle, reg) for reg in old_signature.rights()]
new_i_soqs = [_reg_to_soq(LeftDangle, reg) for reg in new_signature.lefts()]
soq_map: List[Tuple[SoquetT, SoquetT]] = list(zip(old_i_soqs, new_i_soqs))

# Then we reverse the order of subbloqs
bloqnections = reversed(list(cbloq.iter_bloqnections()))

# And add subbloq.adjoint() back in for each subbloq.
bb, _ = BloqBuilder.from_signature(new_signature)
old_i_soqs = [_reg_to_soq(RightDangle, reg) for reg in old_signature.rights()]
new_i_soqs = [bb._reg_to_qvar(LeftDangle, reg) for reg in new_signature.lefts()]
soq_map: List[Tuple[_SoquetT, QVarT]] = list(zip(old_i_soqs, new_i_soqs))
for binst, preds, succs in bloqnections:
# Instead of get_me returning the right element of a predecessor connection,
# it's the left element of a successor connection.
Expand Down
6 changes: 3 additions & 3 deletions qualtran/_infra/composite_bloq.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
"bb, _ = BloqBuilder.from_signature(cbloq.signature)\n",
"\n",
"# We'll have to \"map\" the soquets from our template cbloq to our new one\n",
"soq_map: List[Tuple[SoquetT, SoquetT]] = []\n",
"soq_map = bb.initial_soq_map(cbloq.signature.lefts())\n",
" \n",
"# Iteration yields each bloq instance as well as its input and output soquets.\n",
"for binst, in_soqs, old_out_soqs in cbloq.iter_bloqsoqs():\n",
Expand Down Expand Up @@ -512,8 +512,8 @@
"# Go through and decompose each subbloq\n",
"# We'll manually code this up in this notebook since this isn't a useful operation.\n",
"bb, _ = BloqBuilder.from_signature(flat_three_p.signature)\n",
"soq_map: List[Tuple[SoquetT, SoquetT]] = []\n",
" \n",
"soq_map: List[Tuple[SoquetT, SoquetT]] = bb.initial_soq_map(flat_three_p.signature.lefts())\n",
"\n",
"for binst, in_soqs, old_out_soqs in flat_three_p.iter_bloqsoqs():\n",
" in_soqs = bb.map_soqs(in_soqs, soq_map)\n",
" \n",
Expand Down
Loading
Loading