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

Postselect with default.mixed #6711

Open
ankit27kh opened this issue Dec 12, 2024 · 5 comments
Open

Postselect with default.mixed #6711

ankit27kh opened this issue Dec 12, 2024 · 5 comments
Labels
enhancement ✨ New feature or request

Comments

@ankit27kh
Copy link
Contributor

Feature details

Currently postselection is only available on default.qubit. It would we great to have it on other devices like default.mixed to work with noise

Implementation

No response

How important would you say this feature is?

3: Very important! Blocking work.

Additional information

I am currently resorting to getting the density matrix and manually calculating the postselected outputs. Can this be made possible in the backend?

@ankit27kh ankit27kh added the enhancement ✨ New feature or request label Dec 12, 2024
@CatalinaAlbornoz
Copy link
Contributor

Hi @ankit27kh , thank you for opening this issue. Do you have a code example of what you're having to do? I'm thinking that might help our developers investigate the issue and possible solutions.

@ankit27kh
Copy link
Contributor Author

Hi @CatalinaAlbornoz, this is what I meant:

This works:

import pennylane.numpy as pnp
import pennylane as qml

pnp.random.seed(0)
dev = qml.device('default.qubit', wires=2, shots=None) # note the device

params = pnp.random.random(4 ** 2 - 1)


@qml.qnode(dev)
def circ():
    qml.ArbitraryUnitary(params, wires=range(2))
    qml.measure(1, postselect=0) # postselect
    return qml.expval(qml.PauliZ(0))


print(circ())

0.2903383019701829

This does not work:

import pennylane.numpy as pnp
import pennylane as qml

pnp.random.seed(0)
dev = qml.device('default.mixed', wires=2, shots=None) # note the device

params = pnp.random.random(4 ** 2 - 1)


@qml.qnode(dev)
def circ():
    qml.ArbitraryUnitary(params, wires=range(2))
    qml.measure(1, postselect=0) # postselect
    return qml.expval(qml.PauliZ(0))


print(circ())

ValueError: Postselection is not supported on the <LegacyDeviceFacade: <DefaultMixed device (wires=2, shots=None) at 0x7fe8880e8950>> device.

I can get the same answer by:

import pennylane.numpy as pnp
import pennylane as qml

pnp.random.seed(0)
dev = qml.device('default.mixed', wires=2, shots=None) # note the device

params = pnp.random.random(4 ** 2 - 1)

@qml.qnode(dev)
def circ():
    qml.ArbitraryUnitary(params, wires=range(2))
    # qml.measure(1, postselect=0) # postselect removed
    return qml.density_matrix(wires=range(2))


dm = circ()
I = pnp.identity(2)
zero = pnp.array([1, 0]).reshape(-1, 1)
I_P0 = pnp.kron(I, zero @ zero.T)
Z = qml.matrix(qml.PauliZ(0))
ZI = pnp.kron(Z, I)

post_selected_dm = (I_P0 @ dm @ I_P0) / pnp.trace(I_P0 @ dm)
print(pnp.trace(post_selected_dm @ ZI))

(0.2903383019701825+1.7965601084057088e-16j)

@CatalinaAlbornoz
Copy link
Contributor

Thanks for the code examples @ankit27kh !

Our team is looking into supporting noise in statevector simulators next year so that you can simulate quantum channels probabilistically on them. This is already possible with the Qrack device. You can see how to use this in our demo on Digital zero-noise extrapolation (ZNE) with Catalyst.

Would this work for your needs Ankit?

@ankit27kh
Copy link
Contributor Author

Hi, is postselection possible on this device?
I tired the linked example but am unable to get it to work.
Ran this:

import numpy as np
import pennylane as qml

n_wires = 3

np.random.seed(42)

n_layers = 5
template = qml.SimplifiedTwoDesign
weights_shape = template.shape(n_layers, n_wires)
w1, w2 = [2 * np.pi * np.random.random(s) for s in weights_shape]


def circuit(w1, w2):
    template(w1, w2, wires=range(n_wires))
    qml.adjoint(template)(w1, w2, wires=range(n_wires))
    return qml.expval(qml.PauliZ(0))


noiseless_device = qml.device("qrack.simulator", n_wires, noise=0)

ideal_value = qml.QNode(circuit, device=noiseless_device)(w1, w2)
print(f"Ideal value: {ideal_value}")

after doing pip install -U pennylane-qrack
and getting this error:

OSError: /home/ankit/miniconda3/envs/autoencoder/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.32' not found (required by /usr/lib/qrack/libqrack_pinvoke.so)

After this I also tried to install:

    $ sudo add-apt-repository ppa:wrathfulspatula/vm6502q
    $ sudo apt update
    $ sudo apt install libqrack-dev

but no change.

@CatalinaAlbornoz
Copy link
Contributor

Hi @ankit27kh ,

It looks like postselection is not supported on the Qrack device, sorry.

I'm not sure what's the installation issue though. I used pip install pennylane-catalyst pennylane-qrack both locally and on Google Colab and it worked with no issues.
From a quick search here it looks like it might be a gcc issue.
I'd recommend making a new environment if possible.
If this doesn't work it might be best to contact the Qrack team directly on the Qrack repo or on the #qrack channel of the Unitary Fund Discord. Let us know if you have any trouble reaching out to them.

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

No branches or pull requests

2 participants