pyquil.quil

Module for creating and defining Quil programs.

class pyquil.quil.Program(*instructions)

Bases: object

alloc()

Get a new qubit.

Returns:A qubit.
Return type: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
Return type: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
Returns:

The Program instance.

Return type:

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.
Returns:

The Program instance

Return type:

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.
Returns:

The Program instance

Return type:

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
Return type: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.
Returns:

The Quil Program with the branching instructions added.

Return type:

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
Returns: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.
Returns:

The Quil Program with the appropriate measure instruction appended, e.g. MEASURE 0 [1]

Return type:

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.
Returns: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
Return type:string
pop()

Pops off the last instruction.

Returns:The instruction that was popped.
Return type: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.
Returns:

The Quil Program with the loop instructions added.

Return type:

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
Returns:a single pyQuil program
Return type: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