Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
qml.counts
overflow error for more than 64 qubits (#5556)
**Context:** `qml.counts` was converting samples to integers and using f-strings to format as binary strings, however, it caused wires > 64 to have overflow. Without the changes in this PR: ```python import pennylane as qml import numpy as np np.random.seed(582) num_wires = 70 wires = list(range(num_wires)) dev = qml.device("default.clifford", wires=wires, shots=100) @qml.qnode(dev) # @qml.defer_measurements def circuit(): [qml.PauliX(w) for w in wires] return qml.counts() circuit() ``` ```pycon {'-000000000000000000000000000000000000000000000000000000000000000000001': tensor(100, requires_grad=True)} ``` With the changes: ```python import pennylane as qml import numpy as np np.random.seed(582) num_wires = 70 wires = list(range(num_wires)) dev = qml.device("default.clifford", wires=wires, shots=100) @qml.qnode(dev) # @qml.defer_measurements def circuit(): [qml.PauliX(w) for w in wires] return qml.counts() circuit() ``` ```pycon {'1111111111111111111111111111111111111111111111111111111111111111111111': tensor(100, requires_grad=True)} ``` **Description of the Change:** * Change `qml.counts` to convert directly from list of samples to binary string without first converting to integers. * Changed `QubitDevice._samples_to_counts` to cast to `int8` instead of `int64`. This is only for casting samples, not converting to ints, as `int8` is more memory efficient. **Benefits:** * `qml.counts` now works with arbitrary wires. This is useful for devices like `default.clifford`, which can simulate hundreds of qubits. **Possible Drawbacks:** **Related GitHub Issues:** #5537 , #5513 --------- Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
- Loading branch information