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

Segmentation fault in cudaq.observe() when the state/ket is not a power of 2 #2490

Open
4 tasks done
wsttiger opened this issue Dec 31, 2024 · 1 comment · May be fixed by #2491
Open
4 tasks done

Segmentation fault in cudaq.observe() when the state/ket is not a power of 2 #2490

wsttiger opened this issue Dec 31, 2024 · 1 comment · May be fixed by #2491
Assignees

Comments

@wsttiger
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

When a vector of size N is passed to cudaq.observe() and N is not a power of 2, the software segfaults rather that give a descriptive error message.

Steps to reproduce the bug

import numpy as np
import cudaq
from cudaq import spin

cudaq.set_target('qpp-cpu')

n_size = 7

@cudaq.kernel
def initial_state(vec: list[float]):
    q = cudaq.qvector(vec)

x = np.random.randn(n_size)
x = x / np.linalg.norm(x)

psi = cudaq.get_state(initial_state, x)
print(f'psi: {psi}')
print(f'x: {x}')

H = -5.0 + 2.5 * spin.x(0) * spin.y(1) + 0.23 * spin.z(1) + 0.11 * spin.z(0)

result = cudaq.observe(initial_state, H, x).expectation()
print(f'result: {result}')

Expected behavior

Should give a meaningful error message to alert the user of the power of 2 requirement

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

Not a regression

Environment

  • CUDA-Q version:
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response

@sacpis sacpis self-assigned this Dec 31, 2024
@sacpis sacpis linked a pull request Dec 31, 2024 that will close this issue
@bmhowe23
Copy link
Collaborator

bmhowe23 commented Jan 6, 2025

It would be advantageous if the error check could also handle a mismatch in data length vs the number of qubits. I.e. throw an error if the user provides a vector of length 4 to initialize 4 qubits. If possible, this may need to be done in the simulators themselves (like in the addQubitsToState calls where state is non-null.

The exact number to compare against would need to be simulator-dependent. I.e. 2^n for state-vector-based simulators and 2^(2*n) for density matrix based simulators.

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

Successfully merging a pull request may close this issue.

3 participants