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

[Python] Handle more conditions in MidCircuitMeasurementAnalyzer #2701

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions python/cudaq/kernel/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def visit_Assign(self, node):
node.value, 'id') and node.value.id in self.measureResultsVars:
self.measureResultsVars.append(target.id)
return
# Check if the new variable is assigned from a measurement result
if hasattr(node, 'value') and isinstance(
node.value,
ast.Name) and node.value.id in self.measureResultsVars:
self.measureResultsVars.append(target.id)
return
# Check if the new variable uses measurement results
if hasattr(node, 'value') and isinstance(
node.value, ast.BoolOp) and 'values' in node.value.__dict__:
for value in node.value.__dict__['values']:
if hasattr(value, 'id') and value.id in self.measureResultsVars:
self.measureResultsVars.append(target.id)
return
if not 'func' in node.value.__dict__:
return
creatorFunc = node.value.func
Expand All @@ -54,8 +67,14 @@ def getVariableName(self, node):
return ''

def checkForMeasureResult(self, value):
return self.isMeasureCallOp(value) or self.getVariableName(
value) in self.measureResultsVars
if self.isMeasureCallOp(value):
return True
if self.getVariableName(value) in self.measureResultsVars:
return True
if isinstance(value, ast.BoolOp) and 'values' in value.__dict__:
for val in value.__dict__['values']:
if self.getVariableName(val) in self.measureResultsVars:
return True

def visit_If(self, node):
condition = node.test
Expand Down
43 changes: 43 additions & 0 deletions python/tests/mlir/ast_conditionals.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,49 @@ def test14():

# CHECK-LABEL: func.func @__nvqpp__mlirgen__test14() attributes {"cudaq-entrypoint", "cudaq-kernel", qubitMeasurementFeedback = true} {

@cudaq.kernel
def test15():
qubits = cudaq.qvector(2)
h(qubits)
foo = mx(qubits[0])
bar = foo

if not bar:
reset(qubits[0])

print(test15)

# CHECK-LABEL: func.func @__nvqpp__mlirgen__test15() attributes {"cudaq-entrypoint", "cudaq-kernel", qubitMeasurementFeedback = true} {

@cudaq.kernel
def test16():
qubits = cudaq.qvector(2)
x(qubits)
foo = mx(qubits[0])
bar = my(qubits[1])
qux = foo or bar

if qux:
h(qubits[0])

print(test16)

# CHECK-LABEL: func.func @__nvqpp__mlirgen__test16() attributes {"cudaq-entrypoint", "cudaq-kernel", qubitMeasurementFeedback = true} {

@cudaq.kernel
def test17():
qubits = cudaq.qvector(2)
x(qubits)
foo = mx(qubits[0])
bar = my(qubits[1])

if not foo and bar:
h(qubits[0])

print(test17)

# CHECK-LABEL: func.func @__nvqpp__mlirgen__test17() attributes {"cudaq-entrypoint", "cudaq-kernel", qubitMeasurementFeedback = true} {


# leave for gdb debugging
if __name__ == "__main__":
Expand Down
Loading