Skip to content

observe has unexpected behavior with an if statement and mz #3509

@mmvandieren

Description

@mmvandieren

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

The mz gives an error message if it is in an unexecuted if statement when using observe. However, if instead the kernel refers to that mz command by calling a sub kernel then no error message is flagged. See the working and broken code examples.

use case: the if statement was prudent if I wanted to have just one kernel that I needed to sample a subset of qubits, but I also wanted to compute expectation values for. for instance in a variational quantum random walk with auxiliary qubits

Steps to reproduce the bug

import cudaq

@cudaq.kernel()
def broken_kernel(with_measure : int):
q=cudaq.qvector(2)
if with_measure == 1:
mz(q)

hamiltonian = cudaq.spin.z(0) + cudaq.spin.z(1)

#the next line breaks the code
result = cudaq.observe(broken_kernel, hamiltonian, 0).expectation()

#but if we nest the mz command into a separate kernel, we don't get an error message
@cudaq.kernel()
def measure(qubits: cudaq.qview):
mz(qubits)

@cudaq.kernel
def working_kernel(with_measure : int):
q=cudaq.qvector(2)
if with_measure == 1:
measure(q)

result = cudaq.observe(working_kernel, hamiltonian, 0).expectation()

Expected behavior

expected that both versions of the kernel would either both give errors or both work ok (both working ok would be preferred)

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

Not a regression

Environment

  • CUDA-Q version: cu12-0.10.0
  • Python version: 3.10.12
  • C++ compiler:
  • Operating system: Mac OS

Suggestions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions