Skip to content

Commit 826ee53

Browse files
fix #41
1 parent 2e415d2 commit 826ee53

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
- Fix to use `status` for `circuit.sample` when `allow_state=True`.
4242

43+
- Fix sample bug when number of qubit exceeding 32.
44+
4345
### Changed
4446

4547
- The order of arguments of `tc.timeevol.ed_evol` are changed for consistent interface with other evolution methods.

tensorcircuit/basecircuit.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
correlation_from_counts,
2020
measurement_counts,
2121
sample_int2bin,
22-
sample_bin2int,
2322
sample2all,
2423
)
2524
from .abstractcircuit import AbstractCircuit
@@ -619,8 +618,8 @@ def perfect_sampling(key: Any) -> Any:
619618
if format is None:
620619
return r
621620
r = backend.stack([ri[0] for ri in r]) # type: ignore
622-
r = backend.cast(r, "int32")
623-
ch = sample_bin2int(r, self._nqubits)
621+
ch = backend.cast(r, "int32")
622+
# ch = sample_bin2int(r, self._nqubits)
624623
else: # allow_state
625624
if batch is None:
626625
nbatch = 1

tensorcircuit/quantum.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
from functools import partial, reduce
1111
from operator import matmul, mul, or_
12+
from collections import Counter
1213
from typing import (
1314
Any,
1415
Callable,
@@ -2886,6 +2887,17 @@ def sample2all(
28862887
:return: measurement results specified as ``format``
28872888
:rtype: Any
28882889
"""
2890+
if n > 32:
2891+
assert (
2892+
len(backend.shape_tuple(sample)) == 2
2893+
), "n>32 is only supported for ``sample_bin``"
2894+
if format == "sample_bin":
2895+
return sample
2896+
if format == "count_dict_bin":
2897+
binary_strings = ["".join(map(str, shot)) for shot in sample]
2898+
return dict(Counter(binary_strings))
2899+
raise ValueError(f"n={n} is too large for measurement representaion: {format}")
2900+
28892901
if len(backend.shape_tuple(sample)) == 1:
28902902
sample_int = sample
28912903
sample_bin = sample_int2bin(sample, n)

0 commit comments

Comments
 (0)