Skip to content

Conversation

rturrado
Copy link
Contributor

Context:
We can investigate the option of passing down source code information like variable names from the frontend down to the generated ir, using MLIR's location mechanism. The additional info will make generated code more readable.

Description of the Change:

  1. User -> Frontend: @qjit(use_nameloc=True) to use name location information in the generated MLIR code.

  2. Frontend -> Catalyst CLI: whenever use_nameloc is enabled:

    • Pass stdin=self.mlir_module.operation.get_asm(enable_debug_info=True) instead of stdin=str(self.mlir_module).
    • Pass --use-nameloc-as-prefix in options.
  3. Catalyst CLI -> frontend: whenever --use-nameloc-as-prefix is enabled:

    • Enable useNameLocAsPrefix printing flag.
    • Print the MLIR module to stdout with printing flags, e.g., mlirModule->print(oss, opPrintingFlags) instead of oss << *mlirModule.

Benefits:

  • @qjit(use_nameloc=True) is an eay way for the user to specify that they want to use name location information.
  • The same applies to Catalyst CLI's --use-nameloc-as-prefix option.

Possible Drawbacks:

  • get_arg_names function may not be robust enough.
  • Name location information works nicely for function parameter names (e.g., %x instead of %arg0), but maybe not so nicely for results of equations (e.g., %jit28ok292Fjit28jit_f292Fmul instead of %0). An option would be to add name location information only for function parameters.

Related GitHub Issues:
#1714

rturrado and others added 11 commits August 26, 2025 13:27
Add test_alice_and_bob_parameters.
Rename test_alice to test_alice_qubit.
Rename test_alice_and_bob to test_alice_and_bob_qubits.
lower_jaxpr_to_mlir: add arg_names, and pass them on to jaxpr_to_mlir.
jaxpr_to_mlir: change parameter order, add arg_names, and pass them on to custom_lower_jaxpr_to_module.
custom_lower_jaxpr_to_module: add arg_names, and pass them on to lower_jaxpr_to_fun. This latter function builds arg_locs with the arg_names and appends them to the function's entry_block.

qjit: add use_nameloc parameter.
CompileOptions: add use_nameloc option.
_options_to_cli_flags: add '-mlir-use-nameloc-as-prefix' if use_nameloc=True.

test_debug.py: add test_option_use_nameloc.Add frontend/test/lit/test_option_use_nameloc.py.

TODO: the name location information is added to the MLIR module, but still not consumed by quantum-opt.
TODO: should we try adding name location information for other than funcion arguments?
… name location information in Catalyst

Add QJIT.get_arg_names() to construct a list of argument names for a qjit function.
Change QJIT.mlir and QJIT.mlir_opt to optionally use an MLIR string with debug info.
Change canonicalize function to optionally pass --use-nameloc-as-prefix.

Add --use-nameloc-as-prefix to Catalyst CLI options.
Change QuantumDriverMain to optionally use useNameLocAsPrefix printing flag when printing the MLIR module.

Add test_get_arg_names.py.
Change test_option_use_nameloc.py to check that:
1) MLIR module contains location information.
2) MLIR code contains location information.
3) f.mlir uses the qjit function parameter names, %x and %y, instead of %arg0 and %arg1.
@github-actions github-actions bot added the external PRs where the author is not a part of PennyLane Org (or part of external contributors team) label Sep 18, 2025
@rturrado rturrado changed the title Issue 1714 add variable names to the ir Issue #1714: add variable names to the IR Sep 19, 2025
@rturrado rturrado changed the title Issue #1714: add variable names to the IR Issue #1714: Add variable names to the IR Sep 19, 2025
@mlxd
Copy link
Member

mlxd commented Sep 25, 2025

Thanks @rturrado for the submission.
We are currently finalizing our plans for Q4, so may take a little longer to get around to reviewing this.
Thanks in advance for your patience.

@rturrado
Copy link
Contributor Author

Thanks @rturrado for the submission.
We are currently finalizing our plans for Q4, so may take a little longer to get around to reviewing this.
Thanks in advance for your patience.

Many thanks for the update, @mlxd. No rush at all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external PRs where the author is not a part of PennyLane Org (or part of external contributors team)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants