pyquil.quil

Module for creating and defining Quil programs.

class pyquil.quil.Program(*instructions)[source]

Bases: pyquil.quilbase.InstructionGroup

dagger(inv_dict=None, suffix='-INV')[source]

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)[source]

Define a new static gate.

Parameters:
  • name (string) – The name of the gate.
  • matrix (array-like) – List of lists or Numpy 2d array.
Returns:

The Program instance.

Return type:

Program

get_qubits()[source]
Returns:a set of all the qubit indices allocated in this program, synthesizing freely

allocated qubits if neccessary. :rtype: set

if_then(classical_reg, if_program, else_program=None)[source]

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

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

measure(qubit_index, classical_reg)[source]

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)[source]

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
out()[source]

Converts the Quil program to a readable string.

Returns:String form of a program
Return type:string
synthesize(resource_manager=None)[source]
while_do(classical_reg, q_program)[source]

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

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)[source]

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