# OpenSquirrel: a flexible quantum program compiler.

[](https://pypi.org/project/opensquirrel/)
[](https://pypi.python.org/pypi/opensquirrel)
[](https://github.com/astral-sh/ruff)
[](https://github.com/pytest-dev/pytest)
[](https://opensource.org/licenses/Apache-2.0)
```
,;;:;,
;;;;;
,:;;:; ,'=.
;:;:;' .=" ,'_\
':;:;,/ ,__:=@
';;:; =./)_
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](https://qutech-delft.github.io/cQASM-spec/latest/) quantum programming language,
using [libQASM](https://github.com/QuTech-Delft/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).
```shell
$ pip install opensquirrel
```
To install the dependencies to run the examples on `jupyter`, install:
```shell
$ pip install opensquirrel[examples]
```
## Getting started
Once installed, the `opensquirrel` module can be imported accordingly:
```python
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`:
```python
import math
from opensquirrel.circuit_builder import CircuitBuilder
from opensquirrel.ir import Qubit
# 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), 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:
```python
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](https://github.com/QuTech-Delft/OpenSquirrel/tree/develop/example/tutorials).
In the example below, the circuit is decomposed using the Z-Y-Z decomposer.
```python
from opensquirrel.passes.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.
```python
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](https://QuTech-Delft.github.io/OpenSquirrel/) is hosted through GitHub Pages.
## Contributing
The contribution guidelines and set up can be found
[here](https://github.com/QuTech-Delft/OpenSquirrel/blob/develop/CONTRIBUTING.md).
## Licensing
OpenSquirrel is licensed under the Apache License, Version 2.0. See
[LICENSE](https://github.com/QuTech-Delft/OpenSquirrel/blob/master/LICENSE.md) for the full license text.
## Authors
Quantum Inspire: [support@quantum-inspire.com](mailto:"support@quantum-inspire.com")
Raw data
{
"_id": null,
"home_page": "https://github.com/QuTech-Delft/OpenSquirrel",
"name": "opensquirrel",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "quantum, circuits, compilation",
"author": "Quantum Inspire",
"author_email": "support@quantum-inspire.com",
"download_url": "https://files.pythonhosted.org/packages/15/78/2cc9d88a3f46715ffd339205f8a201f2981a7569702e3eb065f314f20387/opensquirrel-0.3.1.tar.gz",
"platform": null,
"description": "# OpenSquirrel: a flexible quantum program compiler.\n\n\n[](https://pypi.org/project/opensquirrel/)\n[](https://pypi.python.org/pypi/opensquirrel)\n[](https://github.com/astral-sh/ruff)\n[](https://github.com/pytest-dev/pytest)\n[](https://opensource.org/licenses/Apache-2.0)\n\n```\n ,;;:;,\n ;;;;;\n ,:;;:; ,'=.\n ;:;:;' .=\" ,'_\\\n ':;:;,/ ,__:=@\n ';;:; =./)_\n jgs `\"=\\_ )_\"`\n ``'\"`\n```\n\nOpenSquirrel is a quantum compiler that chooses a _modular_, over a _configurable_,\napproach to prepare and optimize quantum circuits for heterogeneous target architectures.\n\nIt has a user-friendly interface and is straightforwardly extensible with custom-made readers,\ncompiler passes, and exporters.\nAs a quantum circuit compiler,\nit is fully aware of the semantics of each gate and arbitrary quantum gates can be constructed manually.\nIt supports the [cQASM](https://qutech-delft.github.io/cQASM-spec/latest/) quantum programming language,\nusing [libQASM](https://github.com/QuTech-Delft/libqasm) as language parser.\nIt is developed in modern Python and follows best practices.\n\n## Installation\n\nOpenSquirrel can be easily installed from PyPI.\nWe recommend using a virtual environment (_e.g._, venv).\n\n```shell\n$ pip install opensquirrel\n```\n\nTo install the dependencies to run the examples on `jupyter`, install:\n\n```shell\n$ pip install opensquirrel[examples]\n```\n\n## Getting started\n\nOnce installed, the `opensquirrel` module can be imported accordingly:\n\n```python\nimport opensquirrel\n```\n\nEssentially, compiling a quantum circuit in OpenSquirrel can be seen as a 3-stage process:\n1. Defining and building the quantum circuit using either the `CircuitBuilder` or from a cQASM string.\n2. Executing (multiple) compilation passes on the circuit,\neach traversing and modifying it (_e.g._, decomposition of the gates).\n3. Writing the circuit to cQASM or exporting it to a specific quantum circuit format.\n\nHere is an example of building a circuit using the `CircuitBuilder`:\n\n```python\nimport math\nfrom opensquirrel.circuit_builder import CircuitBuilder\nfrom opensquirrel.ir import Qubit\n\n# Initialize the builder and build your circuit\nbuilder = CircuitBuilder(qubit_register_size=1)\nbuilder.H(Qubit(0)).Z(Qubit(0)).Y(Qubit(0)).Rx(Qubit(0), math.pi / 3)\n\n# Get the circuit from the circuit builder\nqc = builder.to_circuit()\n```\n\nAlternatively, one can define the same circuit as a cQASM string:\n\n```python\ncqasm_string = (\"\"\"\n version 3.0\n\n qubit q\n\n H q\n Z q\n Y q\n Rx(1.0471976) q\n\"\"\")\n\nfrom opensquirrel.circuit import Circuit\nqc = Circuit.from_string(cqasm_string)\n```\n\nThe circuit can then be decomposed using a decomposition strategy.\nThe different decomposition strategies can be found in the\n[examples](https://github.com/QuTech-Delft/OpenSquirrel/tree/develop/example/tutorials).\nIn the example below, the circuit is decomposed using the Z-Y-Z decomposer.\n\n```python\nfrom opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer\n\nqc.decompose(decomposer=ZYZDecomposer())\n```\n\nOnce the circuit is decomposed, it can be written back to cQASM.\nThis is done by invoking the `writer` class, as can be seen below.\n\n```python\nfrom opensquirrel.writer import writer\n\nwriter.circuit_to_string(qc)\n```\n\nThe output is then given by the following cQASM string:\n\n version 3.0\n\n qubit[1] q\n\n Rz(3.1415927) q[0]\n Ry(1.5707963) q[0]\n Rz(3.1415927) q[0]\n Ry(3.1415927) q[0]\n Rz(1.5707963) q[0]\n Ry(1.0471976) q[0]\n Rz(-1.5707963) q[0]\n\n> __*Note*__: The cQASM writer is the standard writer of OpenSquirrel.\n> 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.\n\n## Documentation\n\nThe [OpenSquirrel documentation](https://QuTech-Delft.github.io/OpenSquirrel/) is hosted through GitHub Pages.\n\n\n## Contributing\n\nThe contribution guidelines and set up can be found\n[here](https://github.com/QuTech-Delft/OpenSquirrel/blob/develop/CONTRIBUTING.md).\n\n\n## Licensing\n\nOpenSquirrel is licensed under the Apache License, Version 2.0. See\n[LICENSE](https://github.com/QuTech-Delft/OpenSquirrel/blob/master/LICENSE.md) for the full license text.\n\n\n## Authors\n\nQuantum Inspire: [support@quantum-inspire.com](mailto:\"support@quantum-inspire.com\")\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A quantum circuit transformation and manipulation tool",
"version": "0.3.1",
"project_urls": {
"Homepage": "https://github.com/QuTech-Delft/OpenSquirrel",
"Repository": "https://github.com/QuTech-Delft/OpenSquirrel"
},
"split_keywords": [
"quantum",
" circuits",
" compilation"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "d0e6462d2c2065ff21c9605f222407bc688d215e47dd12de00bde4200fb930dc",
"md5": "962671a15b12105856089300c5ecbb14",
"sha256": "a48ca8a76e6946db97103438c9a8b78f290ce6ef08a9c491dccd9170c2e50b3b"
},
"downloads": -1,
"filename": "opensquirrel-0.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "962671a15b12105856089300c5ecbb14",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 54550,
"upload_time": "2025-01-31T13:55:14",
"upload_time_iso_8601": "2025-01-31T13:55:14.322365Z",
"url": "https://files.pythonhosted.org/packages/d0/e6/462d2c2065ff21c9605f222407bc688d215e47dd12de00bde4200fb930dc/opensquirrel-0.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "15782cc9d88a3f46715ffd339205f8a201f2981a7569702e3eb065f314f20387",
"md5": "8b0aa9326674a126f7e310c45017d6e7",
"sha256": "f562547042efb0bb22f0bc00b0f0ff1eaa602ca18cf5727a96d236560405e92f"
},
"downloads": -1,
"filename": "opensquirrel-0.3.1.tar.gz",
"has_sig": false,
"md5_digest": "8b0aa9326674a126f7e310c45017d6e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 41112,
"upload_time": "2025-01-31T13:55:16",
"upload_time_iso_8601": "2025-01-31T13:55:16.888220Z",
"url": "https://files.pythonhosted.org/packages/15/78/2cc9d88a3f46715ffd339205f8a201f2981a7569702e3eb065f314f20387/opensquirrel-0.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-31 13:55:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "QuTech-Delft",
"github_project": "OpenSquirrel",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "opensquirrel"
}