catalyst icon indicating copy to clipboard operation
catalyst copied to clipboard

Quantum execution for structured qnode repr is added to the Catalyst

Open rniczh opened this issue 2 months ago • 2 comments

Context:

Hackweek project

Description of the Change:

builtin.module {
  func.func @qnode_main(%shots : i64, %num_qubits : i64, %angle : f64) -> tensor<4xf64> {
    %result = quantum.call_execution(%shots, %num_qubits, %angle) {callee = @execution_1} : (i64, i64, f64) -> tensor<4xf64>
    func.return %result : tensor<4xf64>
  }

  quantum.execution() {function_type = (i64, i64, f64) -> tensor<4xf64>, sym_name = "execution_1"} (
    init {
    ^bb0(%shots : i64, %num_qubits : i64, %angle : f64):
      quantum.device shots(%shots) [
          "...dylib",
          "LightningSimulator",
          "{'mcmc': False, 'num_burnin': 0, 'kernel_name': None}"
      ]
      %qreg = quantum.alloc(%num_qubits) : !quantum.reg
      quantum.exec_yield %qreg, %angle : !quantum.reg, f64
    },
    evolution {
    ^bb1(%qreg_in : !quantum.reg, %rx_angle : f64):
      %q0 = quantum.extract %qreg_in[0] : !quantum.reg -> !quantum.bit
      %q0_rx = quantum.custom "RX"(%rx_angle) %q0 : !quantum.bit
      %qreg_evolved = quantum.insert %qreg_in[0], %q0_rx : !quantum.reg, !quantum.bit
      quantum.exec_yield %qreg_evolved : !quantum.reg
    },
    measurement {
    ^bb2(%qreg_meas : !quantum.reg):
      %obs = quantum.compbasis qreg %qreg_meas : !quantum.obs
      %probs = quantum.probs %obs : tensor<4xf64>
      quantum.exec_yield %qreg_meas, %probs : !quantum.reg, tensor<4xf64>
    },
    teardown {
    ^bb3(%qreg_cleanup : !quantum.reg, %final_result : tensor<4xf64>):
      quantum.dealloc %qreg_cleanup : !quantum.reg
      quantum.device_release
      quantum.exec_yield %final_result : tensor<4xf64>
    }
  )
}

Benefits:

Possible Drawbacks:

Related GitHub Issues:

rniczh avatar Sep 23 '25 20:09 rniczh

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 97.45%. Comparing base (7070359) to head (29aa557). :warning: Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2061   +/-   ##
=======================================
  Coverage   97.45%   97.45%           
=======================================
  Files          90       90           
  Lines       10476    10476           
  Branches      972      972           
=======================================
  Hits        10209    10209           
  Misses        213      213           
  Partials       54       54           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Sep 23 '25 21:09 codecov[bot]

:astonished:

paul0403 avatar Sep 23 '25 22:09 paul0403