PennyLaneAI / PennyLaneAI/catalyst

Print function ordering

Open
#407 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
234
Forks
84
Avg merge
2d 15h
Merged PRs (30d)
66

Description

The print function should not be re-ordered. Example:

import pennylane as qml
from catalyst import debug, grad, measure, qjit


@qml.qnode(qml.device("lightning.qubit", wires=20))
def f(x):
    qml.RX(x, wires=[0])
    debug.print("hello")
    return qml.state()


@qjit(keep_intermediate=True, verbose=True)
def foo(x):
    y = f(x)
    debug.print("world")
    return y


print(foo(1.0))

Produces the following incorrect MLIR

module @foo {
  func.func public @jit_foo(%arg0: tensor<f64>) -> tensor<1048576xcomplex<f64>> attributes {llvm.emit_c_interface} {
    %0 = call @f(%arg0) : (tensor<f64>) -> tensor<1048576xcomplex<f64>>
    "catalyst.print"() {const_val = "world"} : () -> ()
    return %0 : tensor<1048576xcomplex<f64>>
  }
  func.func private @f(%arg0: tensor<f64>) -> tensor<1048576xcomplex<f64>> attributes {diff_method = "parameter-shift", llvm.linkage = #llvm.linkage<internal>, qnode} {
    "catalyst.print"() {const_val = "hello"} : () -> ()
    quantum.device["/home/erick.ochoalopez/Code/cataliist/frontend/catalyst/utils/../../../runtime/build/lib/librtd_lightning.so", "LightningSimulator", "{'shots': 0, 'mcmc': False}"]
    %0 = stablehlo.constant dense<20> : tensor<i64>

Notice that the first statement of the qnode is the print statement as opposed to any other.

The correct MLIR would have the print statement after the RX operation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided Python example and inspect its generated MLIR, focusing on how catalyst.print operations are ordered relative to the RX operation and qnode calls. Done means the generated MLIR preserves source order, with "hello" emitted after RX and "world" after the qnode call.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.