Skip to content

Commit dcfee7b

Browse files
committed
Fix printing for CircuitSpecs
1 parent bc1ff64 commit dcfee7b

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

pennylane/resource/resource.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,22 @@ def __getitem__(self, key):
481481
f"key '{key}' not available. Options are {[field.name for field in fields(self)]}"
482482
)
483483

484+
def _print_resources(self, res) -> str:
485+
"""Helper for printing resources, prints list or single SpecsResources."""
486+
lines = []
487+
if isinstance(res, SpecsResources):
488+
lines.append(res.to_pretty_str(preindent=2))
489+
elif isinstance(res, list):
490+
for i, res in enumerate(res):
491+
lines.append(f" Batched tape {i}:")
492+
lines.append(res.to_pretty_str(preindent=4))
493+
else:
494+
raise ValueError(
495+
"Resources must be either a SpecsResources object or a list of SpecsResources objects."
496+
)
497+
498+
return "\n".join(lines)
499+
484500
# Separate str and repr methods for simple and pretty printing
485501
def __str__(self):
486502
lines = []
@@ -493,14 +509,13 @@ def __str__(self):
493509
lines.append("") # Blank line
494510

495511
lines.append("Resource specifications:")
496-
if isinstance(self.resources, SpecsResources):
497-
lines.append(self.resources.to_pretty_str(preindent=2))
498-
else:
512+
if isinstance(self.resources, dict):
499513
for level, res in self.resources.items():
500514
lines.append(f"Level = {level}:")
501-
lines.append(res.to_pretty_str(preindent=2))
502-
515+
self._print_resources(res)
503516
lines.append("\n" + "-" * 60 + "\n") # Separator between levels
517+
else:
518+
lines.append(self._print_resources(self.resources))
504519

505520
return "\n".join(lines).rstrip("\n-")
506521

tests/resource/test_specs.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,55 @@ def circuit(x):
328328
assert specs_output.num_device_wires is None
329329
assert specs_output.shots == Shots(None)
330330

331+
assert (
332+
str(specs_output)
333+
== """Device: default.qubit
334+
Device wires: None
335+
Shots: Shots(total=None)
336+
Level: 2
337+
338+
Resource specifications:
339+
Batched tape 0:
340+
Total qubit allocations: 2
341+
Total gates: 5
342+
Circuit depth: 5
343+
344+
Gate types:
345+
RandomLayers: 1
346+
RX: 1
347+
SWAP: 1
348+
PauliX: 2
349+
350+
Measurements:
351+
expval(PauliX @ PauliZ): 1
352+
Batched tape 1:
353+
Total qubit allocations: 3
354+
Total gates: 5
355+
Circuit depth: 5
356+
357+
Gate types:
358+
RandomLayers: 1
359+
RX: 1
360+
SWAP: 1
361+
PauliX: 2
362+
363+
Measurements:
364+
expval(PauliZ @ PauliY): 1
365+
Batched tape 2:
366+
Total qubit allocations: 3
367+
Total gates: 5
368+
Circuit depth: 5
369+
370+
Gate types:
371+
RandomLayers: 1
372+
RX: 1
373+
SWAP: 1
374+
PauliX: 2
375+
376+
Measurements:
377+
expval(PauliY @ PauliX): 1"""
378+
)
379+
331380
@pytest.mark.parametrize(
332381
"device,num_wires",
333382
devices_list,

0 commit comments

Comments
 (0)