Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Confusing counts labels for list of MCMs including a postselected MCM #5513

Closed
1 task done
dwierichs opened this issue Apr 15, 2024 · 1 comment · Fixed by #5556
Closed
1 task done

[BUG] Confusing counts labels for list of MCMs including a postselected MCM #5513

dwierichs opened this issue Apr 15, 2024 · 1 comment · Fixed by #5556
Labels
bug 🐛 Something isn't working

Comments

@dwierichs
Copy link
Contributor

Expected behavior

Meaningful strings are created to reference mid-circuit measurement values.

Actual behavior

For a very particular setup, strings are produced that contain a "-" character, which is unexpected and not meaningful.

Note that the output strings should consist of 4 varying integers (first_mcms), 1 integer that is fixed to "1" (postselected_mcm) and 3 more varying integers (second_mcms).

Additional information

No response

Source code

import pennylane as qml
import numpy as np

np.random.seed(582)

dev = qml.device("default.qubit", shots=100, seed=5214)
num_wires = 4
wires = list(range(num_wires))

@qml.qnode(dev)
def circuit(x, y, z):
    [qml.RX(x, w) for w in wires]
    first_mcms = [qml.measure(w, reset=False) for w in wires]
    postselected_mcm = qml.measure(0, postselect=1)
    second_mcms = [qml.measure(w, reset=False) for w in wires[1:]]
    return qml.counts([*first_mcms, postselected_mcm, *second_mcms])

x, y, z = np.random.random(3)
circuit(x, y, z)

Tracebacks

{'-0000001': 1,
 '-0010010': 1,
 '-0100011': 1,
 '-0110100': 3,
 '-1000101': 1,
 '-1010110': 2,
 '-1100111': 3,
 '-1111000': 5}

System information

pl dev

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@dwierichs dwierichs added the bug 🐛 Something isn't working label Apr 15, 2024
@dwierichs dwierichs changed the title [BUG] [BUG] Confusing counts labels for list of MCMs including a postselected MCM Apr 15, 2024
mudit2812 added a commit that referenced this issue Apr 18, 2024
**Context:**
[sc-61314] Issue #5513 was being caused by samples that were converted
to decimals to be cast to `int8`. This caused overflow with 8 or more
wires, resulting in negative values being present in the counts
dictionary.

**Description of the Change:**
* Update `qml.counts.process_samples` and
`QubitDevice._samples_to_counts` to cast to `int64` instead of `int8`.

**Benefits:**
No more overflow issues with `qml.counts`.

**Possible Drawbacks:**

**Related GitHub Issues:**
#5513
@isaacdevlugt
Copy link
Contributor

Just noting here that this issue is still not resolved with #5544 because circuits with more than 64 qubits will have this behaviour still

@mudit2812 mudit2812 linked a pull request Apr 22, 2024 that will close this issue
mudit2812 added a commit that referenced this issue Apr 23, 2024
**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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants