Rigetti computing quantum CLI
Ensure you have Forest installed:
pip install pyquilWhile Forest (pyQuil) does not have a dedicated CLI like Qiskit, you interact with it using Python scripts executed from the command line. Below are some common tasks performed with Forest using Python scripts.
Create a Python script (my_circuit.py) that uses Forest:
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT, MEASURE
# Create a quantum program
p = Program()
ro = p.declare('ro', 'BIT', 2)
# Define a quantum circuit
p += H(0)
p += CNOT(0, 1)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
# Get a quantum computer
qc = get_qc('2q-qvm')
# Run the program
result = qc.run_and_measure(p, trials=10)
print(result)Run the script from the command line:
python my_circuit.py- Simulate a quantum circuit:
from pyquil import Program, get_qc from pyquil.gates import H, CNOT, MEASURE # Create a quantum program p = Program() ro = p.declare('ro', 'BIT', 2) # Define a quantum circuit p += H(0) p += CNOT(0, 1) p += MEASURE(0, ro[0]) p += MEASURE(1, ro[1]) # Get a quantum computer simulator qc = get_qc('2q-qvm') # Run the program result = qc.run_and_measure(p, trials=10) print(result)
Run the simulation script:
python simulate_circuit.py- Run a circuit on a quantum computer:
from pyquil import Program, get_qc from pyquil.gates import H, CNOT, MEASURE # Create a quantum program p = Program() ro = p.declare('ro', 'BIT', 2) # Define a quantum circuit p += H(0) p += CNOT(0, 1) p += MEASURE(0, ro[0]) p += MEASURE(1, ro[1]) # Get a quantum computer (real device) qc = get_qc('Aspen-9') # Run the program result = qc.run_and_measure(p, trials=10) print(result)
-
List available quantum devices:
from pyquil.api import get_devices devices = get_devices() for device in devices: print(device)
-
Get details of a specific device:
from pyquil.api import get_qc qc = get_qc('Aspen-9') print(qc)
- Optimize a quantum circuit:
from pyquil import Program from pyquil.gates import H, X, Y # Create a quantum program p = Program() p += H(0) p += X(0) p += Y(0) # Optimize the program optimized_program = p.optimize() print(optimized_program)
-
Export circuit to Quil:
from pyquil import Program from pyquil.gates import H, MEASURE # Create a quantum program p = Program() p += H(0) p += MEASURE(0) # Export to Quil with open('circuit.quil', 'w') as f: f.write(p.out())
-
Import circuit from Quil:
from pyquil import Program # Import from Quil with open('circuit.quil', 'r') as f: quil_str = f.read() program = Program(quil_str) print(program)
You can also run Forest commands in Jupyter Notebooks for interactive development and visualization.
-
Install Jupyter Notebook:
pip install notebook
-
Start Jupyter Notebook:
jupyter notebook
-
Run Forest in a notebook cell:
from pyquil import Program, get_qc from pyquil.gates import H, MEASURE # Create a quantum program p = Program() p += H(0) p += MEASURE(0) # Get a quantum computer simulator qc = get_qc('1q-qvm') # Run the program result = qc.run_and_measure(p, trials=10) print(result)
-
Set parameters in a quantum program:
from pyquil import Program from pyquil.gates import RX, MEASURE import numpy as np # Create a quantum program with a parameter p = Program() theta = np.pi / 2 p += RX(theta, 0) p += MEASURE(0) print(p)
-
Compile a quantum program:
from pyquil import Program, get_qc from pyquil.gates import H, MEASURE # Create a quantum program p = Program() p += H(0) p += MEASURE(0) # Get a quantum computer qc = get_qc('1q-qvm') # Compile the program executable = qc.compile(p) print(executable)
This comprehensive list covers a wide range of Forest (pyQuil) CLI commands and Python script examples for managing and simulating quantum circuits. It includes basic operations, advanced execution options, device management, optimizations, and more, providing a thorough guide for interacting with Forest from the command line.