Skip to content

QuTech-Delft/OpenSquirrel

Repository files navigation

OpenSquirrel: a flexible quantum program compiler.

CI pypi image Ruff pytest License

 ,;;:;,
   ;;;;;
  ,:;;:;    ,'=.
  ;:;:;' .=" ,'_\
  ':;:;,/  ,__:=@
   ';;:;  =./)_
 jgs `"=\_  )_"`
          ``'"`

OpenSquirrel is a quantum compiler that chooses a modular, over a configurable, approach to prepare and optimize quantum circuits for heterogeneous target architectures.

It has a user-friendly interface and is straightforwardly extensible with custom-made readers, compiler passes, and exporters. As a quantum circuit compiler, it is fully aware of the semantics of each gate and arbitrary quantum gates can be constructed manually. It supports the cQASM quantum programming language, using libQASM as language parser. It is developed in modern Python and follows best practices.

Installation

OpenSquirrel can be easily installed from PyPI. We recommend using a virtual environment (e.g., venv).

$ pip install opensquirrel

To install the dependencies to run the examples on jupyter, install:

$ pip install opensquirrel[examples]

Getting started

Once installed, the opensquirrel module can be imported accordingly:

import opensquirrel

Essentially, compiling a quantum circuit in OpenSquirrel can be seen as a 3-stage process:

  1. Defining and building the quantum circuit using either the CircuitBuilder or from a cQASM string.
  2. Executing (multiple) compilation passes on the circuit, each traversing and modifying it (e.g., decomposition of the gates).
  3. Writing the circuit to cQASM or exporting it to a specific quantum circuit format.

Here is an example of building a circuit using the CircuitBuilder:

import math
from opensquirrel.circuit_builder import CircuitBuilder
from opensquirrel.ir import Qubit, Float

# Initialize the builder and build your circuit
builder = CircuitBuilder(qubit_register_size=1)
builder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), Float(math.pi / 3))

# Get the circuit from the circuit builder
qc = builder.to_circuit()

Alternatively, one can define the same circuit as a cQASM string:

cqasm_string = ("""
    version 3.0

    qubit q

    H q
    Z q
    Y q
    Rx(1.0471976) q
""")

from opensquirrel.circuit import Circuit
qc = Circuit.from_string(cqasm_string)

The circuit can then be decomposed using a decomposition strategy. The different decomposition strategies can be found in the examples. In the example below, the circuit is decomposed using the Z-Y-Z decomposer.

from opensquirrel.decomposer.aba_decomposer import ZYZDecomposer

qc.decompose(decomposer=ZYZDecomposer())

Once the circuit is decomposed, it can be written back to cQASM. This is done by invoking the writer class, as can be seen below.

from opensquirrel.writer import writer

writer.circuit_to_string(qc)

The output is then given by the following cQASM string:

version 3.0

qubit[1] q

Rz(3.1415927) q[0]
Ry(1.5707963) q[0]
Rz(3.1415927) q[0]
Ry(3.1415927) q[0]
Rz(1.5707963) q[0]
Ry(1.0471976) q[0]
Rz(-1.5707963) q[0]

Note: The cQASM writer is the standard writer of OpenSquirrel. This means that the string representation of the Circuit object is by default a cQASM string. Moreover, simply printing the Circuit object will result in its cQASM string representation.

Documentation

The OpenSquirrel documentation is hosted through GitHub Pages.

Contributing

The contribution guidelines and set up can be found here.

Licensing

OpenSquirrel is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Authors

Quantum Inspire: support@quantum-inspire.com