Skip to content

Commit 3c67b37

Browse files
committed
Merge remote-tracking branch 'origin/main' into 2026-02/ruff-compatible
2 parents b417a25 + 9b1bacf commit 3c67b37

4 files changed

Lines changed: 70 additions & 8 deletions

File tree

qualtran/_infra/quantum_graph.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,47 @@ class Connection:
140140
left: Soquet
141141
right: Soquet
142142

143+
@cached_property
144+
def num_qubits(self) -> int:
145+
"""The number of qubits in the connection.
146+
147+
This excludes classical bits.
148+
"""
149+
lq = self.left.reg.dtype.num_qubits
150+
rq = self.right.reg.dtype.num_qubits
151+
152+
if lq != rq:
153+
raise ValueError(f"Invalid Connection {self}: num_qubits mismatch: {lq} != {rq}")
154+
return lq
155+
156+
@cached_property
157+
def num_cbits(self) -> int:
158+
"""The number of classical bits in the connection."""
159+
lc = self.left.reg.dtype.num_cbits
160+
rc = self.right.reg.dtype.num_cbits
161+
162+
if lc != rc:
163+
raise ValueError(f"Invalid Connection {self}: num_cbits mismatch: {lc} != {rc}")
164+
return lc
165+
166+
@cached_property
167+
def num_bits(self) -> int:
168+
"""The number of bits in the connection (quantum + classical)."""
169+
lb = self.left.reg.dtype.num_bits
170+
rb = self.right.reg.dtype.num_bits
171+
172+
if lb != rb:
173+
raise ValueError(f"Invalid Connection {self}: shape mismatch: {lb} != {rb}")
174+
return lb
175+
143176
@cached_property
144177
def shape(self) -> int:
145-
ls = self.left.reg.bitsize
146-
rs = self.right.reg.bitsize
178+
"""The number of bits in the connection (quantum + classical).
147179
148-
if ls != rs:
149-
raise ValueError(f"Invalid Connection {self}: shape mismatch: {ls} != {rs}")
150-
return ls
180+
This is a misleading name for this property kept for backwards compatibility.
181+
Please prefer `.num_bits`.
182+
"""
183+
return self.num_bits
151184

152185
def __str__(self) -> str:
153186
return f'{self.left} -> {self.right}'

qualtran/drawing/graphviz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def add_binst(self, graph: pydot.Graph, binst: BloqInstance) -> pydot.Graph:
312312

313313
def cxn_label(self, cxn: Connection) -> str:
314314
"""Overridable method to return labels for connections."""
315-
return str(cxn.shape)
315+
return str(cxn.num_bits)
316316

317317
def cxn_edge(self, left_id: str, right_id: str, cxn: Connection) -> pydot.Edge:
318318
"""Overridable method to style a pydot.Edge for connecionts."""

qualtran/resource_counting/_qubit_counts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ def _cbloq_max_width(
6565
# During the application of the binst, we have "observer" connections that have
6666
# width as well as the width from the binst itself. We consider the case where
6767
# the bloq may have a max_width greater than the max of its left/right registers.
68-
during_size = _bloq_max_width(binst.bloq) + sum(s.shape for s in in_play)
68+
during_size = _bloq_max_width(binst.bloq) + sum(s.num_qubits for s in in_play)
6969
max_width = smax(max_width, during_size)
7070

7171
# After the binst, its successor connections are 'in play'.
7272
in_play.update(succ_cxns)
73-
after_size = sum(s.shape for s in in_play)
73+
after_size = sum(s.num_qubits for s in in_play)
7474
max_width = smax(max_width, after_size)
7575

7676
return max_width

qualtran/resource_counting/_qubit_counts_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ def test_many_alloc():
9595
assert get_cost_value(bloq, QubitCount()) == 11
9696

9797

98+
def test_qubit_count_ignores_cbits():
99+
import functools
100+
101+
import attr
102+
103+
from qualtran import Bloq, BloqBuilder, CBit, QBit, Register, Signature
104+
105+
@attr.frozen
106+
class MyBloq(Bloq):
107+
n: int
108+
m: int
109+
110+
@functools.cached_property
111+
def signature(self):
112+
return Signature(
113+
[Register('qs', QBit(), shape=(self.n,)), Register('cs', CBit(), shape=(self.m,))]
114+
)
115+
116+
def build_call_graph(self, ssa):
117+
return {}
118+
119+
def build_composite_bloq(self, bb: BloqBuilder, qs, cs):
120+
return {'qs': qs, 'cs': cs}
121+
122+
blq = MyBloq(5, 100)
123+
assert get_cost_value(blq, QubitCount()) == 5
124+
assert get_cost_value(blq.decompose_bloq(), QubitCount()) == 5
125+
126+
98127
@pytest.mark.notebook
99128
def test_notebook():
100129
qlt_testing.execute_notebook("qubit_counts")

0 commit comments

Comments
 (0)