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

Noise model does not support custom op #2316

Open
3 of 4 tasks
Squirtle007 opened this issue Oct 24, 2024 · 2 comments
Open
3 of 4 tasks

Noise model does not support custom op #2316

Squirtle007 opened this issue Oct 24, 2024 · 2 comments

Comments

@Squirtle007
Copy link

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

Currently, noisy simulations do not support user-defined custom operations. Specifically, adding noise channels (e.g., cudaq.BitFlipChannel, cudaq.PhaseFlipChannel, cudaq.KrausChannel) to cudaq.NoiseModel() with a custom op will result in an error:

RuntimeError: Invalid quantum op for noise_model::add_channel

Steps to reproduce the bug

import cudaq
import numpy as np

# Identity matrix as a custom unitary operator
matrix = np.array([[1, 0, 0, 1]])
cudaq.register_operation("id", matrix)


# Let's define a simple kernel that we will add noise to.
qubit_count = 1

@cudaq.kernel
def kernel(qubit_count: int):
    qvector = cudaq.qvector(qubit_count)
    id(qvector)

print(cudaq.draw(kernel, qubit_count))


# Define the Kraus Error Operator as a complex ndarray.
error_probability = 0.2
kraus_0 = np.array([[1.0, 0.0], [0.0, np.sqrt(1 - error_probability)]], dtype=np.complex128)
kraus_1 =  np.array([[0.0, np.sqrt(error_probability)], [0.0, 0.0]], dtype=np.complex128)

# Add the two channels to our Noise Model.
noise_model = cudaq.NoiseModel()

# Add the Kraus Operator to create a quantum noise channel.
damping_channel = cudaq.KrausChannel([kraus_0, kraus_1])

# Apply the damping channel to the custom op specified as "id" on the 1st qubit.
noise_model.add_channel("id", [0], damping_channel)

Expected behavior

cudaq.NoiseModel() should accept custom operator: [str], like "id" defined above by users, as a valid unitary quantum operator, and a subsequent cudaq.sample call can return SampleResult accordingly.

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA Quantum version: 0.8.0
  • Python version: 3.10

Suggestions

No response

@1tnguyen
Copy link
Collaborator

Hi @Squirtle007

The noise for custom op feature will be available in the next release (0.9). It is available now in the nightly docker image.

An example to test:

import cudaq
import numpy as np

cudaq.set_target("density-matrix-cpu")

# Identity matrix as a custom unitary operator
matrix = np.array([[1, 0, 0, 1]])
cudaq.register_operation("id", matrix)


# Let's define a simple kernel that we will add noise to.
qubit_count = 1

@cudaq.kernel
def kernel(qubit_count: int):
    qvector = cudaq.qvector(qubit_count)
    x(qvector)
    id(qvector)


# Define the Kraus Error Operator as a complex ndarray.
error_probability = 0.2
kraus_0 = np.array([[1.0, 0.0], [0.0, np.sqrt(1 - error_probability)]], dtype=np.complex128)
kraus_1 =  np.array([[0.0, np.sqrt(error_probability)], [0.0, 0.0]], dtype=np.complex128)

# Add the two channels to our Noise Model.
noise_model = cudaq.NoiseModel()

# Add the Kraus Operator to create a quantum noise channel.
damping_channel = cudaq.KrausChannel([kraus_0, kraus_1])

# Apply the damping channel to the custom op specified as "id" on the 1st qubit.
noise_model.add_channel("id", [0], damping_channel)

sample = cudaq.sample(kernel, qubit_count, noise_model=noise_model)
print(sample)

This will print a noisy result like { 0:211 1:789 }.

Note: in the kernel, I added an X operation to set the qubit to 1 so that the noise effect can be observed as we are using an amplitude damping channel which only affects excited states.

@Squirtle007
Copy link
Author

Thanks, @1tnguyen , this works on my end using the CUDA-Q nightly image. Thanks for your confirmation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants