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 (list[Union[MeasurementProcess, Operator]]) – terminal measurements, in temporal order
wires (.Wires) – The addressable wire registers of the device that will be executing this graph
par_info (Optional[list[dict]]) – Parameter information. For each index, the entry is a dictionary containing an operation
parameters. (and an index into that operation's)
trainable_params (Optional[set[int]]) – A set containing the indices of parameters that support differentiability. The indices provided match the order of appearance in the quantum circuit.
Attributes
The graph representation of the quantum circuit.
Creating a hash for the circuit graph based on the string generated by serialize.
Returns the maximum number of measurements on any wire in the circuit graph.
Observables in the circuit.
Observables in the circuit, in a fixed topological order.
Operations in the circuit.
Operations in the circuit, in a fixed topological order.
Identify the parametrized layer structure of the circuit.
- graph¶
The graph representation of the quantum circuit.
The graph has nodes representing indices into the queue, and directed edges pointing from nodes to their immediate dependents/successors.
- Returns:
the directed acyclic graph representing the quantum circuit
- Return type:
rustworkx.PyDiGraph
- 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.X(0)) >>> qnode = qml.QNode(circuit_measure_max_once, dev) >>> tape = qml.workflow.construct_tape(qnode)() >>> tape.graph.max_simultaneous_measurements 1 >>> def circuit_measure_max_twice(): ... return qml.expval(qml.X(0)), qml.probs(wires=0) >>> qnode = qml.QNode(circuit_measure_max_twice, dev) >>> tape = qml.workflow.construct_tape(qnode)() >>> tape.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[Union[MeasurementProcess, Operator]]
- 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 everyOperator
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]
Methods
ancestors
(ops[, sort])Ancestors of a given set of operators.
ancestors_in_order
(ops)Operator ancestors in a topological order.
ancestors_of_indexes
(indexes[, sort])Ancestors of a given set of operators.
descendants
(ops[, sort])Descendants of a given set of operators.
descendants_in_order
(ops)Operator descendants in a topological order.
descendants_of_indexes
(indexes[, sort])Descendants of a given set of operators.
Depth of the quantum circuit (longest path in the DAG).
has_path
(a, b)Checks if a path exists between the two given nodes.
has_path_idx
(a_idx, b_idx)Checks if a path exists between the two given nodes.
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_in_order(ops)[source]¶
Operator ancestors in a topological order.
Currently the topological order is determined by the queue index.
- ancestors_of_indexes(indexes, sort=False)[source]¶
Ancestors of a given set of operators.
- Parameters:
indexes (Sequence[int]) – the index into the queue for the operator
sort=False (bool) – if
True
, sort the operators accordingindex (to the topological order determined by the queue)
- Returns:
ancestors of the given operators
- Return type:
list[Operator]
- descendants_in_order(ops)[source]¶
Operator descendants in a topological order.
Currently the topological order is determined by the queue index.
- descendants_of_indexes(indexes, sort=False)[source]¶
Descendants of a given set of operators.
- Parameters:
indexes (Sequence[int]) – the index into the queue for the operator
sort=False (bool) – if
True
, sort the operators accordingindex (to the topological order determined by the queue)
- Returns:
descendants of the given operators
- Return type:
list[Operator]
- has_path_idx(a_idx, b_idx)[source]¶
Checks if a path exists between the two given nodes.
- Parameters:
a_idx (int) – initial node index
b_idx (int) – final node index
- Returns:
returns
True
if a path exists- Return type:
bool
- 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 froma
vias
tob
iff the set is nonempty. The endpoints belong to the path.
- 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