qml.CircuitGraph

class CircuitGraph(ops, variable_deps)[source]

Bases: object

Represents a quantum circuit as a directed acyclic graph.

In this representation the Operator instances are the nodes of the graph, and each directed edge represent a subsystem (or a group of subsystems) on which the two Operators act subsequently. This representation can describe the causal relationships between arbitrary quantum channels and measurements, not just unitary gates.

Parameters
  • ops (Iterable[Operator]) – quantum operators constituting the circuit, in temporal order

  • variable_deps (dict[int, list[ParameterDependency]]) – Free parameters of the quantum circuit. The dictionary key is the parameter index.

diagonalizing_gates

Returns the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.

graph

The graph representation of the quantum circuit.

hash

Creating a hash for the circuit graph based on the string generated by serialize.

is_sampled

Returns True if the circuit graph contains observables which are sampled.

observables

Observables in the circuit, in a fixed topological order.

observables_in_order

Observables in the circuit, in a fixed topological order.

operations

Operations in the circuit, in a fixed topological order.

operations_in_order

Operations in the circuit, in a fixed topological order.

parametrized_layers

Identify the parametrized layer structure of the circuit.

diagonalizing_gates

Returns the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.

Returns

the operations that diagonalize the observables

Return type

List[Operation]

graph

The graph representation of the quantum circuit.

The graph has nodes representing Operator instances, and directed edges pointing from nodes to their immediate dependents/successors.

Returns

the directed acyclic graph representing the quantum circuit

Return type

networkx.DiGraph

hash

Creating a hash for the circuit graph based on the string generated by serialize.

Returns

the hash of the serialized quantum circuit graph

Return type

int

is_sampled

Returns True if the circuit graph contains observables which are sampled.

observables

Observables in the circuit, in a fixed topological order.

The topological order used by this method is guaranteed to be the same as the order in which the measured observables are returned by the quantum function. Currently the topological order is determined by the queue index.

Returns

observables

Return type

list[Observable]

observables_in_order

Observables in the circuit, in a fixed topological order.

The topological order used by this method is guaranteed to be the same as the order in which the measured observables are returned by the quantum function. Currently the topological order is determined by the queue index.

Returns

observables

Return type

list[Observable]

operations

Operations in the circuit, in a fixed topological order.

Currently the topological order is determined by the queue index.

The complement of QNode.observables(). Together they return every Operator instance in the circuit.

Returns

operations

Return type

list[Operation]

operations_in_order

Operations in the circuit, in a fixed topological order.

Currently the topological order is determined by the queue index.

The complement of QNode.observables(). Together they return every Operator instance in the circuit.

Returns

operations

Return type

list[Operation]

parametrized_layers

Identify the parametrized layer structure of the circuit.

Returns

layers of the circuit

Return type

list[Layer]

ancestors(ops)

Ancestors of a given set of operators.

ancestors_in_order(ops)

Operator ancestors in a topological order.

descendants(ops)

Descendants of a given set of operators.

descendants_in_order(ops)

Operator descendants in a topological order.

draw([charset, show_variable_names])

Draw the CircuitGraph as a circuit diagram.

greedy_layers()

Greedily collected layers of the circuit.

invisible_operations()

Operations that cannot affect the circuit output.

iterate_parametrized_layers()

Parametrized layers of the circuit.

nodes_between(a, b)

Nodes on all the directed paths between the two given nodes.

print_contents()

Prints the contents of the quantum circuit.

serialize()

Serialize the quantum circuit graph based on the operations and observables in the circuit graph and the index of the variables used by them.

update_node(old, new)

Replaces the given circuit graph node with a new one.

wire_indices(wire)

Operator indices on the given wire.

ancestors(ops)[source]

Ancestors of a given set of operators.

Parameters

ops (Iterable[Operator]) – set of operators in the circuit

Returns

ancestors of the given operators

Return type

set[Operator]

ancestors_in_order(ops)[source]

Operator ancestors in a topological order.

Currently the topological order is determined by the queue index.

Parameters

ops (Iterable[Operator]) – set of operators in the circuit

Returns

ancestors of the given operators, topologically ordered

Return type

list[Operator]

descendants(ops)[source]

Descendants of a given set of operators.

Parameters

ops (Iterable[Operator]) – set of operators in the circuit

Returns

descendants of the given operators

Return type

set[Operator]

descendants_in_order(ops)[source]

Operator descendants in a topological order.

Currently the topological order is determined by the queue index.

Parameters

ops (Iterable[Operator]) – set of operators in the circuit

Returns

descendants of the given operators, topologically ordered

Return type

list[Operator]

draw(charset='unicode', show_variable_names=False)[source]

Draw the CircuitGraph as a circuit diagram.

Parameters
  • charset (str, optional) – The charset that should be used. Currently, “unicode” and “ascii” are supported.

  • show_variable_names (bool, optional) – Show variable names instead of variable values.

Raises

ValueError – If the given charset is not supported

Returns

The circuit diagram representation of the CircuitGraph

Return type

str

greedy_layers()[source]

Greedily collected layers of the circuit. Empty slots are filled with None.

Layers are built by pushing back gates in the circuit as far as possible, so that every Gate is at the lower possible layer.

Returns

Tuple of the circuits operations and the circuits observables, both indexed by wires.

Return type

Tuple[list[list[Operation]], list[list[Observable]]]

invisible_operations()[source]

Operations that cannot affect the circuit output.

An Operation instance in a quantum circuit is invisible if is not an ancestor of an observable. Such an operation cannot affect the circuit output, and usually indicates there is something wrong with the circuit.

Returns

operations that cannot affect the output

Return type

set[Operator]

iterate_parametrized_layers()[source]

Parametrized layers of the circuit.

Returns

layers with extra metadata

Return type

Iterable[LayerData]

nodes_between(a, b)[source]

Nodes on all the directed paths between the two given nodes.

Returns the set of all nodes s that fulfill \(a \le s \le b\). There is a directed path from a via s to b iff the set is nonempty. The endpoints belong to the path.

Parameters
Returns

nodes on all the directed paths between a and b

Return type

set[Operator]

print_contents()[source]

Prints the contents of the quantum circuit.

serialize()[source]

Serialize the quantum circuit graph based on the operations and observables in the circuit graph and the index of the variables used by them.

The string that is produced can be later hashed to assign a unique value to the circuit graph.

Returns

serialized quantum circuit graph

Return type

string

update_node(old, new)[source]

Replaces the given circuit graph node with a new one.

Parameters
Raises

ValueError – if the new Operator does not act on the same wires as the old one

wire_indices(wire)[source]

Operator indices on the given wire.

Parameters

wire (int) – wire to examine

Returns

indices of operators on the wire, in temporal order

Return type

list[int]

Contents

Using PennyLane

Development

API