# pyquil.quil¶

Module for creating and defining Quil programs.

class pyquil.quil.Program(*instructions)

Bases: object

alloc()

Get a new qubit.

Returns: A qubit. Qubit
dagger(inv_dict=None, suffix='-INV')

Creates the conjugate transpose of the Quil program. The program must not contain any irreversible actions (measurement, control flow, qubit allocation).

Returns: The Quil program’s inverse Program
defgate(name, matrix, parameters=None)

Define a new static gate.

Note

The matrix elements along each axis are ordered by bitstring. For two qubits the order is 00, 01, 10, 11, where the the bits are ordered in reverse by the qubit index, i.e., for qubits 0 and 1 the bitstring 01 indicates that qubit 0 is in the state 1. See also the related documentation section in the QVM Overview.

Parameters: name (string) – The name of the gate. matrix (array-like) – List of lists or Numpy 2d array. parameters (list) – list of parameters that are used in this gate The Program instance. Program
define_noisy_gate(name, qubit_indices, kraus_ops)

Overload a static ideal gate with a noisy one defined in terms of a Kraus map.

Note

The matrix elements along each axis are ordered by bitstring. For two qubits the order is 00, 01, 10, 11, where the the bits are ordered in reverse by the qubit index, i.e., for qubits 0 and 1 the bitstring 01 indicates that qubit 0 is in the state 1. See also the related documentation section in the QVM Overview.

Parameters: name (str) – The name of the gate. qubit_indices (tuple|list) – The qubits it acts on. kraus_ops (tuple|list) – The Kraus operators. The Program instance Program
defined_gates

A list of defined gates on the program.

gate(name, params, qubits)

Add a gate to the program.

Note

The matrix elements along each axis are ordered by bitstring. For two qubits the order is 00, 01, 10, 11, where the the bits are ordered in reverse by the qubit index, i.e., for qubits 0 and 1 the bitstring 01 indicates that qubit 0 is in the state 1. See also the related documentation section in the QVM Overview.

Parameters: name (string) – The name of the gate. params (list) – Parameters to send to the gate. qubits (list) – Qubits that the gate operates on. The Program instance Program
get_qubits()

Returns all of the qubit indices used in this program, including gate applications and allocated qubits. e.g.

>>> p = Program()
>>> p.inst(("H", 1))
>>> p.get_qubits()
{1}
>>> q = p.alloc()
>>> p.inst(H(q))
>>> len(p.get_qubits())
2

Returns: A set of all the qubit indices used in this program set
if_then(classical_reg, if_program, else_program=None)

If the classical register at index classical reg is 1, run if_program, else run else_program.

Equivalent to the following construction:

IF [c]:
instrA...
ELSE:
instrB...
=>
JUMP-WHEN @THEN [c]
instrB...
JUMP @END
LABEL @THEN
instrA...
LABEL @END

Parameters: classical_reg (int) – The classical register to check as the condition if_program (Program) – A Quil program to execute if classical_reg is 1 else_program (Program) – A Quil program to execute if classical_reg is 0. This argument is optional and defaults to an empty Program. The Quil Program with the branching instructions added. Program
inst(*instructions)

Mutates the Program object by appending new instructions.

This function accepts a number of different valid forms, e.g.

>>> p = Program()
>>> p.inst(H(0)) # A single instruction
>>> p.inst(H(0), H(1)) # Multiple instructions
>>> p.inst([H(0), H(1)]) # A list of instructions
>>> p.inst(("H", 1)) # A tuple representing an instruction
>>> p.inst("H 0") # A string representing an instruction
>>> q = Program()
>>> p.inst(q) # Another program

It can also be chained:
>>> p = Program()
>>> p.inst(H(0)).inst(H(1))

Parameters: instructions – A list of Instruction objects, e.g. Gates self for method chaining
instructions

Fill in any placeholders and return a list of quil AbstractInstructions.

is_protoquil()

Protoquil programs may only contain gates, no classical instructions and no jumps.

Returns: True if the Program is Protoquil, False otherwise
measure(qubit_index, classical_reg=None)

Measures a qubit at qubit_index and puts the result in classical_reg

Parameters: qubit_index (int) – The address of the qubit to measure. classical_reg (int) – The address of the classical bit to store the result. The Quil Program with the appropriate measure instruction appended, e.g. MEASURE 0 [1] Program
measure_all(*qubit_reg_pairs)

Measures many qubits into their specified classical bits, in the order they were entered.

Parameters: qubit_reg_pairs (Tuple) – Tuples of qubit indices paired with classical bits. The Quil Program with the appropriate measure instructions appended, e.g.
MEASURE 0 [1]
MEASURE 1 [2]
MEASURE 2 [3]

Return type: Program
no_noise()

Prevent a noisy gate definition from being applied to the immediately following Gate instruction.

Returns: Program
out()

Converts the Quil program to a readable string.

Returns: String form of a program string
pop()

Pops off the last instruction.

Returns: The instruction that was popped. tuple
while_do(classical_reg, q_program)

While a classical register at index classical_reg is 1, loop q_program

Equivalent to the following construction:

WHILE [c]:
instr...
=>
LABEL @START
JUMP-UNLESS @END [c]
instr...
JUMP @START
LABEL @END

Parameters: classical_reg (int) – The classical register to check q_program (Program) – The Quil program to loop. The Quil Program with the loop instructions added. Program
pyquil.quil.merge_programs(prog_list)

Merges a list of pyQuil programs into a single one by appending them in sequence

Parameters: prog_list (list) – A list of pyquil programs a single pyQuil program Program
pyquil.quil.shift_quantum_gates(program, shift_offset)

Shifts a quantum gates in a quil program so that all qubit indices change by a certain offset :param program: a pyquil Program :param shift_offset: integer :return: pyquil Program with shifted qubit indices