qoqo

Qoqo

Quantum Operation Quantum Operation
Yes we use reduplication.

qoqo is the HQS python package to represent quantum circuits.

Circuit

Circuit of Operations.

operations

Operations are the atomic instructions in any quantum program that can be represented by qoqo.

measurements

Measurements

QuantumProgram

Represents a quantum program evaluating measurements based on a one or more free float parameters.

Classes

Circuit

Circuit of Operations.

CircuitDag(node_number, edge_number, /)

Represents the Direct Acyclic Graph (DAG) of a Circuit.

QuantumProgram

Represents a quantum program evaluating measurements based on a one or more free float parameters.

class qoqo.Circuit

Circuit of Operations.

A quantum program is represented as a linear sequence of Operations.

add(op)

Add an Operation to Circuit.

Parameters:

op (Operation) – The Operation to add to the Circuit.

count_occurences(operations)

Count the number of occurences of a set of operation tags in the circuit.

Parameters:

operations (list[str]) – List of operation tags that should be counted.

Returns:

The number of occurences of these operation tags.

Return type:

int

definitions()

Return a list of definitions in the Circuit.

Definitions need to be unique.

Returns:

A vector of the definitions in the Circuit.

Return type:

list[Operation]

filter_by_tag(tag)

Return a list of operations with given tag.

Parameters:

tag (str) – tag by which to filter operations.

Returns:

A vector of the operations with the specified tag in the Circuit.

Return type:

list[Operation]

static from_bincode(input)

Convert the bincode representation of the Circuit to a Circuit using the [bincode] crate.

Parameters:

input (ByteArray) – The serialized Circuit (in [bincode] form).

Returns:

The deserialized Circuit.

Return type:

Circuit

Raises:
  • TypeError – Input cannot be converted to byte array.

  • ValueError – Input cannot be deserialized to Circuit.

static from_json(json_string)

Convert the json representation of a Circuit to a Circuit.

Parameters:

input (str) – The serialized Circuit in json form.

Returns:

The deserialized Circuit.

Return type:

Circuit

Raises:

ValueError – Input cannot be deserialized to Circuit.

get(index)

Return a copy of the Operation at a certain index of the Circuit.

Parameters:

index (int) – The index of the Operation to get in the Circuit.

Returns:

The operation at the given index (if it exists).

Return type:

Operation

Raises:

IndexError – Index out of range.

get_operation_types()

Return a list of the hqslang names of all operations occuring in the circuit.

Returns:

The operation types in the Circuit.

Return type:

set[str]

get_slice()

Return the copy of a slice of the Circuit.

Parameters:
  • start (Optional[int]) – The starting index of the slice (inclusive).

  • stop (Optional[int]) – The stopping index of the slice (exclusive).

Returns:

The slice of the operations in the Circuit with the specified indices.

Return type:

Circuit

Raises:
  • IndexError – Stop index smaller than start index.

  • IndexError – Stop index out of range.

  • IndexError – Start index out of range.

operations()

Return a list of all operations in the Circuit.

Returns:

A vector of the operations in the Circuit.

Return type:

list[Operation]

overrotate()

Return clone of the circuit with all overrotation Pragmas applied.

Returns:

Circuit with the overrotation applied

Return type:

Circuit

Raises:

RuntimeError – Error applying PragmaOverrotation in circuit.

Example:

>>> circuit = Circuit()
>>> circuit += PragmaOverrotation("RotateY", [1,], 20.0, 30.0)
>>> circuit += RotateX(0, 0.0)
>>> circuit += RotateY(0, 1.0)
>>> circuit += RotateY(1, 2.0)
>>> circuit += RotateY(1, 3.0)
>>> circuit_overrotated = circuit.overrotate()
print(circuit)
print(circuit_overrotated)
remap_qubits(mapping)

Remap qubits in operations in clone of Circuit.

Parameters:

mapping (dict[int, int]) – The dictionary containing the {qubit: qubit} mapping to use in the Circuit.

Returns:

The Circuit with the qubits remapped.

Return type:

self

Raises:

RuntimeError – The qubit remapping failed.

substitute_parameters(substitution_parameters)

Substitute the symbolic parameters in a clone of the Circuit according to the substitution_parameters input.

Parameters:

substitution_parameters (dict[str, float]) – The dictionary containing the substitutions to use in the Circuit.

Returns:

The Circuit with the parameters substituted.

Return type:

self

Raises:

RuntimeError – The parameter substitution failed.

to_bincode()

Return the bincode representation of the Circuit using the [bincode] crate.

Returns:

The serialized Circuit (in [bincode] form).

Return type:

ByteArray

Raises:

ValueError – Cannot serialize Circuit to bytes.

to_json()

Return the json representation of the Circuit.

Returns:

The serialized form of Circuit.

Return type:

str

Raises:

ValueError – Cannot serialize Circuit to json.

class qoqo.CircuitDag(node_number, edge_number, /)

Represents the Direct Acyclic Graph (DAG) of a Circuit.

add_to_back(op)

Add an Operation to the back of the CircuitDag, if necessary.

Parameters:

op (Operation) – The Operation to add to the back of the CircuitDag.

Raises:

TypeError – The Python Object cannot be converted to Operation.

add_to_front(op)

Add an Operation to the front of the CircuitDag, if necessary.

Parameters:

op (Operation) – The Operation to add to the front of the CircuitDag.

Raises:

TypeError – The Python Object cannot be converted to Operation.

blocking_predecessors(already_executed, to_be_executed)

Checks which of the direct predecessors of an Operation in the CircuitDag blocks the execution.

Warning: This method can only be used to determine if an operation can be executed when already_executed is consistent. When the list already_executed is inconsistent (a n operation is reported as executed that could not have been executed yet) this method returning an empty vector does not imply that the to_be_executed operation can be executed.

Parameters:
  • already_executed (list[int]) – List of NodeIndices of Nodes that have already been executed in the Circuit.

  • to_be_executed (int) – NodeIndex of the Operation that should be executed next.

Returns:

List containing the sorted blocking elements.

Return type:

list[int]

commuting_operations()

Returns the list of nodes of commuting operations in CircuitDag.

Returns:

The list of nodes of commuting operations.

Return type:

list[int]

execution_blocked(already_executed, to_be_executed)

Checks if executing an operation is blocked by any not-yet executed operation.

Parameters:
  • already_executed (list[int]) – List of NodeIndices of Nodes that have already been executed in the Circuit.

  • to_be_executed (int) – NodeIndex of the operation that should be executed next.

Returns:

List containing the sorted blocking elements.

Return type:

list[int]

first_operation_involving_classical()

Returns a dictionary where a key is composed by the name and the size of the classical register and its value represents the first node that involves that register.

Returns:

The dictionary of {(str, int), int} elements.

Return type:

dict[(str, int), int]

first_operation_involving_qubit()

Returns a dictionary where a key represents a qubit and its value represents the first node that involves that qubit.

Returns:

The dictionary of {qubit: node} elements.

Return type:

dict[int, int]

first_parallel_block()

Returns a set containing the nodes in the first parallel block.

Returns:

The set of nodes in the first parallel block.

Return type:

set[int]

static from_bincode(input)

Convert the bincode representation of the CircuitDag to a CircuitDag using the [bincode] crate.

Parameters:

input (ByteArray) – The serialized CircuitDag (in [bincode] form).

Returns:

The deserialized CircuitDag.

Return type:

CircuitDag

Raises:
  • TypeError – Input cannot be converted to byte array.

  • ValueError – Input cannot be deserialized to CircuitDag.

from_circuit()

Create a CircuitDag from a given Circuit;

Parameters:

circuit (Circuit) – The Circuit to build the new CircuitDag from.

Returns:

The new CircuitDag.

Return type:

self

get(index)

Given a NodeIndex, returns the Operation contained in the node of the CircuitDag.

Parameters:

index (int) – The index of the node to get from the CircuitDag.

Returns:

The Operation at the given index (if it exists).

Return type:

Operation

Raises:

IndexError – Index out of range.

last_operation_involving_classical()

Returns a dictionary where a key is composed by the name and the size of the classical register and its value represents the last node that involves that register.

Returns:

The dictionary of {(str, int), int} elements.

Return type:

dict[(str, int), int]

last_operation_involving_qubit()

Returns a dictionary where a key represents a qubit and its value represents the last node that involves that qubit.

Returns:

The dictionary of {qubit: node} elements.

Return type:

dict[int, int]

last_parallel_block()

Returns a set containing the nodes in the last parallel block.

Returns:

The set of nodes in the last parallel block.

Return type:

set[int]

new_front_layer(already_executed, current_front_layer, to_be_executed)

Returns a new front-layer after executing an operation from the current front layer.

Returns an error if operation to be executed is not in the current front layer.

Parameters:
  • already_executed (list[int]) – List of NodeIndices of Nodes that have already been executed in the Circuit.

  • current_front_layer (list[int]) – List of NodeIndices in the current front layer ready to be executed if physically possible.

  • to_be_executed (int) – NodeIndex of the operation that should be executed next.

parallel_blocks()

Returns an iterator over the possible parallel blocks in circuit that can be executed simultaneously

Returns an Iterator over Vectors of references to the NodeIndices in the parallel block as well as references to the Operation in the blocks

successors(node)

Returns the list of the successors of a given node in the CircuitDag.

to_bincode()

Return the bincode representation of the CircuitDag using the [bincode] crate.

Returns:

The serialized CircuitDag (in [bincode] form).

Return type:

ByteArray

Raises:

ValueError – Cannot serialize CircuitDag to bytes.

to_circuit()

Transforms the CircuitDag into a Circuit.

class qoqo.QuantumProgram

Represents a quantum program evaluating measurements based on a one or more free float parameters.

The main use of QuantumProgram is to contain a Measurements implementing [crate::measurements::Measure] that measures expectation values or output registers of [crate::Circuit] quantum circuits that contain symbolic parameters. Circuit with symbolic parameters can not be simulated or executed on real hardware. The symbolic parameters need to be replaced with real floating point numbers first. A QuantumProgram contains a list of the free parameters (input_parameter_names) and can automatically replace the parameters with its run methods and return the result.

The QuantumProgram should correspond as closely as possible to a normal multi-parameter function in classical computing that can be called with a set of parameters and returns a result. It is the intended way to interface between normal program code and roqoqo based quantum programs.

static from_bincode(input)

Convert the bincode representation of the QuantumProgram to a QuantumProgram using the [bincode] crate.

Parameters:

input (ByteArray) – The serialized QuantumProgram (in [bincode] form).

Returns:

The deserialized QuantumProgram.

Return type:

QuantumProgram

Raises:
  • TypeError – Input cannot be converted to byte array.

  • ValueError – Input cannot be deserialized to QuantumProgram.

static from_json(input)

Convert the json representation of a QuantumProgram to a QuantumProgram.

Parameters:

input (str) – The serialized QuantumProgram in json form.

Returns:

The deserialized QuantumProgram.

Return type:

QuantumProgram

Raises:

ValueError – Input cannot be deserialized to QuantumProgram.

input_parameter_names()

Returns the input_parameter_names attribute of the qoqo QuantumProgram.

Returns:

List of input parameter names.

measurement()

Returns the measurement attribute of the QuantumProgram as Python object.

Returns:

PyObject corresponding to the qoqo measurement type of the QuantumProgram, i.e. PauliZProduct, CheatedPauliZProduct, Cheated or ClassicalRegister.

run(backend)

Runs the QuantumProgram and returns expectation values.

Runs the quantum programm for a given set of parameters passed in the same order as the parameters listed in input_parameter_names and returns expectation values.

Parameters:
  • backend (Backend) – The backend the program is executed on.

  • parameters (Optional[List[float]) – List of float parameters of the function call in order of input_parameter_names

run_registers(backend)

Runs the QuantumProgram and returns the classical registers of the quantum program.

Runs the quantum programm for a given set of parameters passed in the same order as the parameters listed in input_parameter_names and returns the classical register output. The classical registers usually contain a record of measurement values for the repeated execution of a [crate::Circuit] quantum circuit for real quantum hardware or the readout of the statevector or the density matrix for simulators.

Parameters:
  • backend (Backend) – The backend the program is executed on.

  • parameters (Optional[List[float]) – List of float parameters of the function call in order of input_parameter_names

to_bincode()

Return the bincode representation of the QuantumProgram using the [bincode] crate.

Returns:

The serialized QuantumProgram (in [bincode] form).

Return type:

ByteArray

Raises:

ValueError – Cannot serialize QuantumProgram to bytes.

to_json()

Return the json representation of the QuantumProgram.

Returns:

The serialized form of QuantumProgram.

Return type:

str

Raises:

ValueError – Cannot serialize QuantumProgram to json.