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

Supporting counts from raw samples #2562

Closed
Jaybsoni opened this issue May 11, 2022 · 4 comments
Closed

Supporting counts from raw samples #2562

Jaybsoni opened this issue May 11, 2022 · 4 comments
Labels
enhancement ✨ New feature or request WIP 🚧 Work-in-progress

Comments

@Jaybsoni
Copy link
Contributor

Jaybsoni commented May 11, 2022

Feature details

Given a quantum circuit we can sample the final state it prepares in the computation basis. For example:

dev = qml.device('default.qubit', wires=3, shots=10)

@qml.qnode(dev)
def my_circ():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0,1])
    qml.PauliX(wires=2) 

    return qml.sample()
>>> my_samples = my_circ()
>>> print(my_samples)
[[1 1 1]
 [0 0 1]
 [1 1 1]
 [0 0 1]
 [1 1 1]
 [1 1 1]
 [0 0 1]
 [1 1 1]
 [1 1 1]
 [0 0 1]]
>>>

Samples produces an array containing the results of sampling the circuit. Its shape is (num_shots, num_qubits) in this case it would be (10, 3). In general it could be the case that someone asks for a very larger number of shots on a system with many qubits. In this case, it would be difficult to work with this full array of samples.

To address this we would like to support the ability to pass in a counts=True option to qml.sample such that the QNode upon evaluation returns a dictionary containing each unique state and the number of times it was sampled.

dev = qml.device('default.qubit', wires=3, shots=10)

@qml.qnode(dev)
def my_circ():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0,1])
    qml.PauliX(wires=2) 

    return qml.sample(counts=True)
>>> my_counts = my_circ()
>>> print(my_counts)
{'111':6, '001':4}

Implementation

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

Once the counts are available, we can also return a vector representation of the counts:

my_counts = my_circ()
wires = 3
count_vector = np.zeros(2 ** wires)
for k, v in my_counts.items():
    idx = int(k, 2)
    count_vector[idx] = v
>>> print(count_vector)
array([0., 4., 0., 0., 0., 0., 0., 6.])

In this case the list would have 2^n elements where n is the number of qubits. The element at index i would correspond to the number of samples for the ith basis state.

@Jaybsoni Jaybsoni added enhancement ✨ New feature or request good first issue Good for newcomers labels May 11, 2022
@antalszava antalszava removed the good first issue Good for newcomers label May 12, 2022
@antalszava
Copy link
Contributor

This change will likely require engineering not just qml.sample, but also QubitDevice and the scope of the change may become non-trivial.

@antalszava antalszava added the WIP 🚧 Work-in-progress label Jun 2, 2022
@antalszava
Copy link
Contributor

This change could include creating a new Counts observable return type (in the measurements.py file). The logic for computing statistics in the QubitDevice base class could then be extended such that the Counts observable return type is handled too. The existing machinery for the Sample return type could be utilized.

@theodotk
Copy link
Contributor

theodotk commented Jun 11, 2022

Hello PennyLane team,

I submitted PR 2686 that adds this feature. I based my implementation on editing the sample function in _qubit_device.py, though it was possible to base it on estimate_probability function (or to create something new that seems superfluous to me). Choice of editing the sample function seemed more natural for this task, but please tell me if that's not the best approach.

As for the count_vector idea, it seems to be already realized as qml.probs() (up to the number of shots factor), so I didn't change anything in this regard.

@antalszava , @Jaybsoni , could you please review the PR?

Thanks!

@antalszava
Copy link
Contributor

Closing with #2686 merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request WIP 🚧 Work-in-progress
Projects
None yet
Development

No branches or pull requests

3 participants