Convert Figma logo to code with AI

rigetti logopyquil

A Python library for quantum programming using Quil.

1,396
341
1,396
218

Top Related Projects

5,026

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.

4,228

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.

3,865

Microsoft Quantum Development Kit Samples

Quick Overview

PyQuil is an open-source Python library for quantum programming using Quil, Rigetti's quantum instruction language. It allows users to construct quantum circuits, run them on Rigetti's quantum processors or simulators, and analyze the results. PyQuil is designed to work seamlessly with Rigetti's quantum computing ecosystem.

Pros

  • Intuitive and flexible API for building quantum circuits
  • Supports both gate-based and analog quantum computing
  • Integrates well with other scientific Python libraries like NumPy
  • Provides access to Rigetti's quantum hardware and simulators

Cons

  • Limited to Rigetti's quantum computing ecosystem
  • Steeper learning curve compared to some other quantum computing frameworks
  • Documentation can be sparse or outdated in some areas
  • Performance may be slower compared to lower-level implementations

Code Examples

  1. Creating a simple quantum circuit:
from pyquil import Program
from pyquil.gates import H, CNOT

# Create a program that applies a Hadamard gate to qubit 0 and a CNOT gate with control qubit 0 and target qubit 1
p = Program()
p += H(0)
p += CNOT(0, 1)
  1. Running a quantum program on a simulator:
from pyquil import get_qc
from pyquil.api import WavefunctionSimulator

# Create a quantum computer simulator with 2 qubits
qc = get_qc("2q-qvm")

# Run the program and measure the results
result = qc.run_and_measure(p, trials=1000)
print(result)
  1. Using the Wavefunction Simulator:
wf_sim = WavefunctionSimulator()
wavefunction = wf_sim.wavefunction(p)

# Print the wavefunction
print(wavefunction)

# Calculate the probability of measuring |00>
prob_00 = abs(wavefunction[0])**2
print(f"Probability of measuring |00>: {prob_00}")

Getting Started

To get started with PyQuil, follow these steps:

  1. Install PyQuil using pip:

    pip install pyquil
    
  2. Import the necessary modules:

    from pyquil import Program, get_qc
    from pyquil.gates import *
    
  3. Create a simple quantum program:

    p = Program()
    p += H(0)
    p += MEASURE(0, 0)
    
  4. Run the program on a simulator:

    qc = get_qc("1q-qvm")
    result = qc.run_and_measure(p, trials=100)
    print(result)
    

For more advanced usage and access to Rigetti's quantum hardware, refer to the official PyQuil documentation.

Competitor Comparisons

5,026

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.

Pros of Qiskit

  • Broader ecosystem with support for multiple quantum hardware providers
  • More extensive documentation and educational resources
  • Larger community and more frequent updates

Cons of Qiskit

  • Steeper learning curve due to its comprehensive nature
  • Can be slower for simple tasks compared to PyQuil's streamlined approach

Code Comparison

PyQuil:

from pyquil import Program
from pyquil.gates import H, CNOT

p = Program()
p += H(0)
p += CNOT(0, 1)

Qiskit:

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)

Both frameworks allow for creating quantum circuits with similar ease. PyQuil uses a more imperative style with the += operator, while Qiskit employs a method-chaining approach. The main difference lies in the naming conventions and overall structure of the code.

PyQuil is specifically designed for Rigetti's quantum computers, making it more streamlined for their hardware. Qiskit, on the other hand, offers a more versatile approach, supporting various quantum hardware providers and providing a comprehensive set of tools for quantum computing research and development.

4,228

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.

Pros of Cirq

  • More extensive and flexible circuit manipulation capabilities
  • Broader support for various quantum hardware platforms
  • Larger community and more frequent updates

Cons of Cirq

  • Steeper learning curve for beginners
  • Less integrated with specific hardware (unlike PyQuil's tight integration with Rigetti hardware)

Code Comparison

PyQuil example:

from pyquil import Program
from pyquil.gates import H, CNOT

p = Program()
p += H(0)
p += CNOT(0, 1)

Cirq example:

import cirq

q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
    cirq.H(q0),
    cirq.CNOT(q0, q1)
)

Both examples create a simple quantum circuit with a Hadamard gate followed by a CNOT gate. Cirq uses a more object-oriented approach with explicit qubit objects, while PyQuil uses integer indices to represent qubits. Cirq's syntax is generally more verbose but offers more flexibility in circuit construction and manipulation.

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.

Pros of PennyLane

  • Supports a wider range of quantum hardware and simulators
  • Offers automatic differentiation for quantum-classical hybrid algorithms
  • Provides a more extensive library of built-in quantum operations and templates

Cons of PennyLane

  • Steeper learning curve for beginners due to its more comprehensive feature set
  • May have slower execution times for certain operations compared to PyQuil

Code Comparison

PyQuil example:

from pyquil import Program
from pyquil.gates import H, CNOT

p = Program()
p += H(0)
p += CNOT(0, 1)

PennyLane example:

import pennylane as qml

dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0))

Both frameworks allow for quantum circuit construction, but PennyLane's approach is more focused on quantum-classical hybrid computations and automatic differentiation.

3,865

Microsoft Quantum Development Kit Samples

Pros of Quantum

  • More comprehensive quantum development kit with broader language support (Q#, Python, C#)
  • Larger community and more extensive documentation
  • Integrated with Azure Quantum for cloud-based quantum computing

Cons of Quantum

  • Steeper learning curve, especially for Q# language
  • Less focus on near-term quantum devices compared to PyQuil
  • More complex setup process for local development

Code Comparison

PyQuil:

from pyquil import Program
from pyquil.gates import H, CNOT

p = Program()
p += H(0)
p += CNOT(0, 1)

Quantum (Q#):

operation BellPair() : (Result, Result) {
    use (q1, q2) = (Qubit(), Qubit());
    H(q1);
    CNOT(q1, q2);
    return (M(q1), M(q2));
}

Both examples create a simple Bell pair, but Quantum's Q# code is more verbose and requires explicit qubit allocation and measurement. PyQuil's approach is more concise and pythonic, making it easier for Python developers to get started with quantum programming.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PyQuil: Quantum programming in Python

binder docs docker pepy pypi slack

PyQuil is a Python library for quantum programming using Quil, the quantum instruction language developed at Rigetti Computing. PyQuil serves three main functions:

PyQuil has a ton of other features, which you can learn more about in the docs. However, you can also keep reading below to get started with running your first quantum program!

Quickstart with interactive tutorial notebooks

Without installing anything, you can quickly get started with quantum programming by exploring our interactive Jupyter notebook tutorials and examples. To run them in a preconfigured execution environment on Binder, click the "launch binder" badge at the top of the README or the link here! To learn more about the tutorials and how you can add your own, visit the rigetti/forest-tutorials repository. If you'd rather set everything up locally, or are interested in contributing to pyQuil, continue onto the next section for instructions on installing pyQuil and the Forest SDK.

Installing pyQuil and the Forest SDK

pypi conda-forge conda-rigetti

PyQuil can be installed using conda, pip, or from source. To install it from PyPI (via pip), do the following:

pip install pyquil

To instead install pyQuil from source, do the following from within the repository after cloning it:

pip install -e .

If you choose to use pip, we highly recommend installing pyQuil within a virtual environment.

PyQuil, along with quilc, the QVM, and other libraries, make up what is called the Forest SDK. To make full use of pyQuil, you will need to additionally have installed quilc and the QVM. For more information, check out the docs!

Running your first quantum program

In just a few lines, we can use pyQuil with the Forest SDK to simulate a Bell state!

from pyquil import get_qc, Program
from pyquil.gates import CNOT, H, MEASURE
 
qvm = get_qc('2q-qvm')
 
p = Program()
p += H(0)
p += CNOT(0, 1)
ro = p.declare('ro', 'BIT', 2)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
p.wrap_in_numshots_loop(10)

qvm.run(p).get_register_map()['ro'].tolist()

The output of the above program should look something like the following, the statistics of which are consistent with a two-qubit entangled state.

[[0, 0],
 [1, 1],
 [1, 1],
 [1, 1],
 [1, 1],
 [0, 0],
 [0, 0],
 [1, 1],
 [0, 0],
 [0, 0]]

Using the Forest SDK, you can simulate the operation of a real quantum processor (QPU). If you would like to run on the real QPUs in our lab in Berkeley, you can sign up for an account on Quantum Cloud Services (QCS)!

Joining the Forest community

If you'd like to get involved with pyQuil and Forest, joining the Rigetti Forest Slack Workspace is a great place to start! You can do so by clicking the invite link in the previous sentence, or in the badge at the top of this README. The Slack Workspace is a great place to ask general questions, join high-level design discussions, and hear about updates to pyQuil and the Forest SDK.

To go a step further and start contributing to the development of pyQuil, good first steps are reporting a bug, requesting a feature, or picking up one of the issues with the good first issue or help wanted labels. Once you find an issue to work on, make sure to fork this repository and then open a pull request once your changes are ready. For more information on all the ways you can contribute to pyQuil (along with some helpful tips for developers and maintainers) check out our Contributing Guide!

To see what people have contributed in the past, check out the Changelog for a detailed list of all announcements, improvements, changes, and bugfixes. The Releases page for pyQuil contains similar information, but with links to the pull request for each change and its corresponding author. Thanks for contributing to pyQuil! 🙂

Citing pyQuil, Forest, and Quantum Cloud Services

zenodo

If you use pyQuil, Grove, or other parts of the Forest SDK in your research, please cite the Quil specification using the following BibTeX snippet:

@misc{smith2016practical,
    title={A Practical Quantum Instruction Set Architecture},
    author={Robert S. Smith and Michael J. Curtis and William J. Zeng},
    year={2016},
    eprint={1608.03355},
    archivePrefix={arXiv},
    primaryClass={quant-ph}
}

Additionally, if your research involves taking data on Rigetti quantum processors (QPUs) via the Quantum Cloud Services (QCS) platform, please reference the QCS paper using the following BibTeX snippet:

@article{Karalekas_2020,
    title = {A quantum-classical cloud platform optimized for variational hybrid algorithms},
    author = {Peter J Karalekas and Nikolas A Tezak and Eric C Peterson
              and Colm A Ryan and Marcus P da Silva and Robert S Smith},
    year = 2020,
    month = {apr},
    publisher = {{IOP} Publishing},
    journal = {Quantum Science and Technology},
    volume = {5},
    number = {2},
    pages = {024003},
    doi = {10.1088/2058-9565/ab7559},
    url = {https://doi.org/10.1088%2F2058-9565%2Fab7559},
}

The preprint of the QCS paper is available on arXiv, and the supplementary interactive notebooks and datasets for the paper can be found in the rigetti/qcs-paper repository.

License

PyQuil is licensed under the Apache License 2.0.