# qml.CircuitGraph¶

class CircuitGraph(ops, obs, wires, par_info=None, trainable_params=None)[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

• obs (Iterable[MeasurementProcess]) – terminal measurements, in temporal order

• wires (Wires) – The addressable wire registers of the device that will be executing this graph

• par_info (dict[int, dict[str, Operation or int]]) – Parameter information. Keys are parameter indices (in the order they appear on the tape), and values are a dictionary containing the corresponding operation and operation parameter index.

• trainable_params (set[int]) – A set containing the indices of parameters that support differentiability. The indices provided match the order of appearence in the quantum circuit.

 graph The graph representation of the quantum circuit. hash Creating a hash for the circuit graph based on the string generated by serialize. max_simultaneous_measurements Returns the maximum number of measurements on any wire in the circuit graph. observables Observables in the circuit. observables_in_order Observables in the circuit, in a fixed topological order. operations Operations in the circuit. operations_in_order Operations in the circuit, in a fixed topological order. parametrized_layers Identify the parametrized layer structure of the circuit.
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

max_simultaneous_measurements

Returns the maximum number of measurements on any wire in the circuit graph.

This method counts the number of measurements for each wire and returns the maximum.

Examples

>>> dev = qml.device('default.qubit', wires=3)
>>> def circuit_measure_max_once():
...     return qml.expval(qml.PauliX(wires=0))
>>> qnode = qml.QNode(circuit_measure_max_once, dev)
>>> qnode()
>>> qnode.qtape.graph.max_simultaneous_measurements
1
>>> def circuit_measure_max_twice():
...     return qml.expval(qml.PauliX(wires=0)), qml.probs(wires=0)
>>> qnode = qml.QNode(circuit_measure_max_twice, dev)
>>> qnode()
>>> qnode.qtape.graph.max_simultaneous_measurements
2

Returns

the maximum number of measurements

Return type

int

observables

Observables in the circuit.

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.

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. Operator ancestors in a topological order. Descendants of a given set of operators. Operator descendants in a topological order. draw([charset, wire_order, show_all_wires]) Draw the CircuitGraph as a circuit diagram. Depth of the quantum circuit (longest path in the DAG). greedy_layers([wire_order, show_all_wires]) Greedily collected layers of the circuit. has_path(a, b) Checks if a path exists between the two given nodes. Operations that cannot affect the circuit output. Parametrized layers of the circuit. nodes_between(a, b) Nodes on all the directed paths between the two given nodes. Prints the contents of the quantum circuit. 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', wire_order=None, show_all_wires=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.

• wire_order (Wires or None) – the order (from top to bottom) to print the wires of the circuit

• show_all_wires (bool) – If True, all wires, including empty wires, are printed.

Raises

ValueError – If the given charset is not supported

Returns

The circuit diagram representation of the CircuitGraph

Return type

str

get_depth()[source]

Depth of the quantum circuit (longest path in the DAG).

greedy_layers(wire_order=None, show_all_wires=False)[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.

Parameters
• wire_order (Wires) – the order (from top to bottom) to print the wires of the circuit

• show_all_wires (bool) – If True, all wires, including empty wires, are printed.

Returns

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

Return type

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

has_path(a, b)[source]

Checks if a path exists between the two given nodes.

Parameters
Returns

returns True if a path exists

Return type

bool

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

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]