# qml.tape.TapeCircuitGraph¶

class TapeCircuitGraph(ops, obs, wires)[source]

Bases: pennylane.circuit_graph.CircuitGraph

New circuit graph object. This will eventually grow to replace the existing CircuitGraph; for now, we simply inherit from the current CircuitGraph, and modify the instantiation so that it can be created via the quantum tape.

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

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, show_variable_names]) Draw the CircuitGraph as a circuit diagram. Depth of the quantum circuit (longest path in the DAG). 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. to_openqasm([rotations]) Serialize the circuit as an OpenQASM 2.0 program. 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)

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)

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)

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)

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)

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

get_depth()[source]

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

greedy_layers()

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]]]

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()

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()

Parametrized layers of the circuit.

Returns

Return type

Iterable[LayerData]

nodes_between(a, b)

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()

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.

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

to_openqasm(rotations=True)

Serialize the circuit as an OpenQASM 2.0 program.

Only operations are serialized; all measurements are assumed to take place in the computational basis.

Note

The serialized OpenQASM program assumes that gate definitions in qelib1.inc are available.

Parameters

rotations (bool) – in addition to serializing user-specified operations, also include the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.

Returns

OpenQASM serialization of the circuit

Return type

str

update_node(old, new)

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)

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]