Skip to content

Commit 8183042

Browse files
authored
Merge branch 'main' into qgf_add_k
2 parents 0ec446c + 3abebf0 commit 8183042

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

qualtran/_infra/binst_graph_iterators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def _priority(node: 'BloqInstance') -> int:
4040
return -_ALLOCATION_PRIORITY
4141

4242
signature = node.bloq.signature
43+
if any(reg.dtype.is_symbolic() for reg in signature):
44+
return 0
4345
return total_bits(signature.rights()) - total_bits(signature.lefts())
4446

4547

qualtran/_infra/binst_graph_iterators_test.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import sympy
1515
from attrs import frozen
1616

1717
from qualtran import (
@@ -26,7 +26,7 @@
2626
SoquetT,
2727
)
2828
from qualtran._infra.binst_graph_iterators import greedy_topological_sort
29-
from qualtran.bloqs.basic_gates import CNOT
29+
from qualtran.bloqs.basic_gates import CNOT, IntState, Swap
3030
from qualtran.bloqs.bookkeeping import Allocate, Free
3131

3232

@@ -61,3 +61,24 @@ def test_greedy_topological_sort():
6161
BloqInstance(bloq=Free(dtype=QBit()), i=5),
6262
RightDangle,
6363
]
64+
65+
66+
def test_topo_sort_with_symbolic_registers():
67+
# when _priority returns a symbolic value, networkx will try to use
68+
# it in a comparison and you would get
69+
# TypeError: cannot determine truth value of Relational.
70+
71+
n = sympy.Symbol('n')
72+
bb = BloqBuilder()
73+
74+
# This isn't usually a problem for thru-registers since sympy will
75+
# simplify n-n to zero, which can be compared. Test against a sided
76+
# symbolic register
77+
x = bb.add(IntState(5, n))
78+
79+
y = bb.add_register('y', n)
80+
x, y = bb.add(Swap(n), x=x, y=y)
81+
x, y = bb.add(Swap(n), x=x, y=y)
82+
cbloq = bb.finalize(x=x, y=y)
83+
res = list(greedy_topological_sort(cbloq._binst_graph))
84+
assert len(res) > 0

qualtran/_infra/composite_bloq.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from qualtran.cirq_interop._cirq_to_bloq import CirqQuregInT, CirqQuregT
5454
from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator
5555
from qualtran.simulation.classical_sim import ClassicalValT
56+
from qualtran.symbolics import SymbolicInt
5657

5758
# NDArrays must be bound to np.generic
5859
_SoquetType = TypeVar('_SoquetType', bound=np.generic)
@@ -883,10 +884,10 @@ def add_register_from_dtype(
883884
def add_register(self, reg: Register, bitsize: None = None) -> Union[None, SoquetT]: ...
884885

885886
@overload
886-
def add_register(self, reg: str, bitsize: int) -> SoquetT: ...
887+
def add_register(self, reg: str, bitsize: 'SymbolicInt') -> SoquetT: ...
887888

888889
def add_register(
889-
self, reg: Union[str, Register], bitsize: Optional[int] = None
890+
self, reg: Union[str, Register], bitsize: Optional['SymbolicInt'] = None
890891
) -> Union[None, SoquetT]:
891892
"""Add a new register to the composite bloq being built.
892893

0 commit comments

Comments
 (0)