Skip to main content

Write quantum programs directly in Python

Project description

quPython

pip install qupython

quPython compiles Python functions into quantum programs, executes the programs, and returns the results as bool-like objects.

Initialize a quPython.Qubit object just like any other object and use it inside a @quantum function. These are the only two imports you'll need.

from qupython import Qubit, quantum

@quantum
def random_bit():
    qubit = Qubit()         # Allocate new qubit
    qubit.h()               # Mutate qubit
    return qubit.measure()  # Measure qubit to bool

When you run random_bit, quPython compiles your function to a quantum program, executes it, and returns results.

>>> random_bit()
True

Python-like data management

Create classes for quantum data just as you would conventional data. The following example creates a simple logical qubit class. See the Logical qubit example for a more complete class.

from qupython import Qubit, quantum
from qupython.typing import BitPromise

class LogicalQubit:
    """
    Simple logical qubit using the five-qubit code.
    See https://en.wikipedia.org/wiki/Five-qubit_error_correcting_code
    """
    def __init__(self):
        """
        Create new logical qubit and initialize to logical |0>.
        Uses initialization procedure from https://quantumcomputing.stackexchange.com/a/14449
        """
        self.qubits = [Qubit() for _ in range(5)]
        self.qubits[4].z()
        for q in self.qubits[:4]:
            q.h()
            with q.as_control():
                self.qubits[4].x()
        for a, b in [(0,4),(0,1),(2,3),(1,2),(3,4)]:
            with self.qubits[b].as_control():
                self.qubits[a].z()

    def measure(self) -> BitPromise:
        """
        Measure logical qubit to single classical bit
        """
        out = Qubit().h()
        for q in self.qubits:
            with out.as_control():
                q.z()
        return out.h().measure()

This abstracts the bit-level operations away from the user.

@quantum
def logical_qubit_demo() -> BitPromise:
    q = LogicalQubit()
    return q.measure()
>>> logical_qubit_demo()
False

Generate Qiskit circuits

If you want, you can just use quPython to create Qiskit circuits with Pythonic syntax (rather than the assembly-like syntax of qc.cx(0, 1) in native Qiskit).

# Compile using quPython
logical_qubit_demo.compile()

# Draw compiled Qiskit circuit
logical_qubit_demo.circuit.draw()
     ┌───┐                                                       
q_0: ┤ H ├────────────■────────■───────────■─────■───────────────
     ├───┤            │        │           │     │               
q_1: ┤ H ├──■─────────┼────────┼──■──■──■──┼─────┼───────────────
     ├───┤  │         │        │  │  │  │  │     │               
q_2: ┤ H ├──┼────■────┼────────┼──┼──■──┼──■──■──┼───────────────
     ├───┤┌─┴─┐┌─┴─┐┌─┴─┐┌───┐ │  │     │     │  │               
q_3: ┤ Z ├┤ X ├┤ X ├┤ X ├┤ X ├─┼──■──■──┼─────┼──┼─────■─────────
     ├───┤└───┘└───┘└───┘└─┬─┘ │     │  │     │  │     │ ┌───┐┌─┐
q_4: ┤ H ├─────────────────┼───┼─────┼──■─────■──■──■──■─┤ H ├┤M├
     ├───┤                 │   │     │              │    └───┘└╥┘
q_5: ┤ H ├─────────────────■───■─────■──────────────■──────────╫─
     └───┘                                                     ║ 
c: 1/══════════════════════════════════════════════════════════╩═
                                                               0 

You can compile the function without executing it, optimize the cirucit, execute it however you like, then use quPython to interpret the results.

from qiskit_aer.primitives import Sampler
qiskit_result = Sampler().run(logical_qubit_demo.circuit).result()
logical_qubit_demo.interpret_result(qiskit_result)  # returns `False`

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

qupython-0.2.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

qupython-0.2.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file qupython-0.2.0.tar.gz.

File metadata

  • Download URL: qupython-0.2.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for qupython-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b4384f4fd76b0c99f1b65433748a6905da3052a8d9faf93f72a9e639755a6536
MD5 72efab0137fb2a91c954c5a18ccf5b61
BLAKE2b-256 7426e941be27d5630f65e6fc01e7ad4bc292a6e584eb177d3715aa79bac64ec0

See more details on using hashes here.

File details

Details for the file qupython-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: qupython-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for qupython-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1d69de568d0eaea0e87d0cdecfa0ee31ad7d587ec079b92801e9c2d3be034c9
MD5 58759778c3e07515c7f0becf42806a2b
BLAKE2b-256 02ea16b3fb7752b3d9e45bfd53dde573e04796b0ade299c1a63f4e59f9f360bc

See more details on using hashes here.

Supported by

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