Skip to main content

Quantinuum's tool for converting HUGR to QIR

Project description

hugr-qir

build_status codecov

A tool for converting Hierarchical Unified Graph Representation (HUGR, pronounced hugger) formatted quantum programs into QIR format.

[!NOTE] Not all Guppy/HUGR programs can be converted to QIR.

Installation

You can install from pypi via pip install hugr-qir.

Usage

Python

Use the function hugr_to_qir from the hugr_to_qir module to convert hugr to qir. By default, some basic validity checks will be run on the generated QIR. These checks can be turned off by passing validate_qir = False.

You can find an example notebook at examples/submit-guppy-h2-via-qir.ipynb showing the conversion and the submission to Quantinuum System Model H2.

CLI

You can use the available CLI after installing the python package.

This will generate qir for a given hugr file:

hugr-qir test-file.hugr

Run hugr-qir --help to see the available options.

If you want to generate a hugr file from guppy, you can do this in two steps:

  1. Add this to the end of your guppy file:

    if __name__ == "__main__":
        sys.stdout.buffer.write(main.compile().to_bytes())
        # Or to compile a non-main guppy function:
        sys.stdout.buffer.write(guppy_func.compile_function().to_bytes())
    
  2. Generate the hugr file with:

    python guppy_examples/general/quantum-classical-1.py > test-guppy.hugr
    

Guppy language support

✅ = full support, *️⃣ = partial support, ❌ = unsupported

Overview Data Types

Data Types Support Caveats
int
bool
nat
struct Cannot contain arrays
float *️⃣ Must be runtime constant, arithmetic comptime only
array *️⃣ Comptime only
tuple *️⃣ Unpacking with * returns array, so only at comptime

Arrays

  • Only supported within comptime guppy
  • Cannot use guppy builtins that use runtime arrays internally
  • Cannot be used within structs
  • Cannot be used as parameter to either @guppy or @guppy.comptime decorated functions

Array examples:

def py_function(arr: array[qubit]) -> None: # ✅ no need to fully qualify array type, will be treated as python list
   """This python function can be called from @guppy.comptime, but not @guppy
      Can do anything here that is allowed within @guppy.comptime
   """
    for q in arr:
       h(q)

@guppy.comptime
def guppy_comptime_function(arr: array[qubit, 4]) -> None: # ❌ no support for passing in arrays (even to guppy.comptime functions)
   for q in arr:
      h(q)

@guppy
def guppy_function(arr: array[qubit, 4]) -> None: # ❌ no support for passing in arrays
   for q in arr:
      h(q)

@guppy.comptime
def main() -> None:
    comptime_array = array(qubit() for _ in range(4)) # ✅: array initialization at comptime
    py_function(comptime_array)             # ✅: can call python function from comptime and pass in comptime array
    guppy_comptime_function(comptime_array) # ❌: parameters to guppy comptime functions are NOT comptime
    guppy_function(comptime_array)          # ❌: parameters to guppy functions are NOT comptime

@guppy
def main_noncomptime() -> None:
   array = array(qubit() for _ in range(4)) # ❌: non-comptime array initialization

Tuples

  • Unpacking with * only supported at comptime (creates array)

Structs

  • Cannot contain arrays

Overview Guppy Features

Features Support Remarks
if elif else constructs
function overloading
Generics (type_var/nat_var) nat_vars not really useful without runtime array support
First class/ Higher order functions
Recursive functions/loops *️⃣ Only if unrollable/serializable
measure_array/discard_array Use non-comptime arrays internally

Development

#️⃣ Setting up the development environment

The easiest way to set up the development environment is to use the provided devenv.nix file. This will set up a development shell with all the required dependencies.

To use this, you will need to install devenv. Once you have it running, open a shell with:

devenv shell

All the required dependencies should be available. You can automate loading the shell by setting up direnv.

Run tests

You can run the rust test with:

cargo test

You can run the Python test with:

pytest -n auto

If you want to update the snapshots you can do that via:

pytest --snapshot-update

License

This project is licensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hugr_qir-0.0.24.tar.gz (817.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

hugr_qir-0.0.24-cp310-abi3-win_amd64.whl (29.3 MB view details)

Uploaded CPython 3.10+Windows x86-64

hugr_qir-0.0.24-cp310-abi3-manylinux_2_34_x86_64.whl (34.3 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.34+ x86-64

hugr_qir-0.0.24-cp310-abi3-macosx_13_0_x86_64.whl (32.3 MB view details)

Uploaded CPython 3.10+macOS 13.0+ x86-64

hugr_qir-0.0.24-cp310-abi3-macosx_13_0_arm64.whl (29.6 MB view details)

Uploaded CPython 3.10+macOS 13.0+ ARM64

File details

Details for the file hugr_qir-0.0.24.tar.gz.

File metadata

  • Download URL: hugr_qir-0.0.24.tar.gz
  • Upload date:
  • Size: 817.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for hugr_qir-0.0.24.tar.gz
Algorithm Hash digest
SHA256 4020efa4910dffca39b4884e76d39028f029fca91f22ab1004bb2acc463f206b
MD5 2046586b0d81d538d94ea5c115b89e13
BLAKE2b-256 177e0f54539ac875390d173f559adce2699e18c7d6716f1ab37b3ad4111a8ccf

See more details on using hashes here.

File details

Details for the file hugr_qir-0.0.24-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for hugr_qir-0.0.24-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3a2f50d0db4913ebba96ffe79a3c20e9313c06df1bad0b81c487703bb2cd79b4
MD5 fc52443bb9b256dbccf8005c7f36d599
BLAKE2b-256 0e58eb3d21519a443a02221c45fe7854593a55ed2328b572862dd245e29b96af

See more details on using hashes here.

File details

Details for the file hugr_qir-0.0.24-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for hugr_qir-0.0.24-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 45a15a06a46797cd0fefa47c849cc0160ceb49e9caaeda70760b16c8b2614da5
MD5 65311dd5c7b00678ca9cf9dd8c0f5832
BLAKE2b-256 d9aeb48bf3650f21045390df326587437f53186a44e6f233c00ff4341bf83e15

See more details on using hashes here.

File details

Details for the file hugr_qir-0.0.24-cp310-abi3-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for hugr_qir-0.0.24-cp310-abi3-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 aba90157d3ca92ce707e8dda1642c679acd4b56ddb42024175fa12fa9d1e644b
MD5 5e8a091d919f5519d1a895dd2f553cdc
BLAKE2b-256 52786cc469e571d2130f31e61aeaea916e605bc0dbac22792f372c759cfeb6a6

See more details on using hashes here.

File details

Details for the file hugr_qir-0.0.24-cp310-abi3-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for hugr_qir-0.0.24-cp310-abi3-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 0bbd5b0f3a0cd1cc415c7886d3822e24bf09684dbcb383caad096e789a8fa682
MD5 97e8ac332dbab9d049f16739e55f8cea
BLAKE2b-256 aa10712c3a1df70b9dff625449dda05736af43882af7c32625186a82ce2bd83b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page