cirq.CircuitDag

class cirq.CircuitDag(can_reorder: Callable[[cirq.Operation, cirq.Operation], bool] = <function _disjoint_qubits>, incoming_graph_data: Any = None, device: cirq.devices.device.Device = cirq.UNCONSTRAINED_DEVICE)[source]

A representation of a Circuit as a directed acyclic graph.

Nodes of the graph are instances of Unique containing each operation of a
circuit.
Edges of the graph are tuples of nodes. Each edge specifies a required
application order between two operations. The first must be applied before
the second.

The graph is maximalist (transitive completion).

__init__(can_reorder: Callable[[cirq.Operation, cirq.Operation], bool] = <function _disjoint_qubits>, incoming_graph_data: Any = None, device: cirq.devices.device.Device = cirq.UNCONSTRAINED_DEVICE) → None[source]

Initializes a CircuitDag.

Parameters
  • can_reorder

    A predicate that determines if two operations may be reordered. Graph edges are created for pairs of operations where this returns False.

    The default predicate allows reordering only when the operations don’t share common qubits.

  • incoming_graph_data – Data in initialize the graph. This can be any value supported by networkx.DiGraph() e.g. an edge list or another graph.

  • device – Hardware that the circuit should be able to run on.

Methods

add_cycle(nodes, **attr)

add_edge(u_of_edge, v_of_edge, **attr)

Add an edge between u and v.

add_edges_from(ebunch_to_add, **attr)

Add all the edges in ebunch_to_add.

add_node(node_for_adding, **attr)

Add a single node node_for_adding and update node attributes.

add_nodes_from(nodes_for_adding, **attr)

Add multiple nodes.

add_path(nodes, **attr)

add_star(nodes, **attr)

add_weighted_edges_from(ebunch_to_add[, weight])

Add weighted edges in ebunch_to_add with specified weight attr

adjacency()

Returns an iterator over (node, adjacency dict) tuples for all nodes.

all_operations()

all_qubits()

append(op)

clear()

Remove all nodes and edges from the graph.

copy([as_view])

Returns a copy of the graph.

disjoint_qubits(op1, op2)

Returns true only if the operations have qubits in common.

edge_subgraph(edges)

Returns the subgraph induced by the specified edges.

findall_nodes_until_blocked(is_blocker)

Finds all nodes before blocking ones.

fresh_copy()

from_circuit(circuit[, can_reorder])

from_ops(*operations[, can_reorder, device])

get_edge_data(u, v[, default])

Returns the attribute dictionary associated with edge (u, v).

has_edge(u, v)

Returns True if the edge (u, v) is in the graph.

has_node(n)

Returns True if the graph contains the node n.

has_predecessor(u, v)

Returns True if node u has predecessor v.

has_successor(u, v)

Returns True if node u has successor v.

is_directed()

Returns True if graph is directed, False otherwise.

is_multigraph()

Returns True if graph is a multigraph, False otherwise.

make_node(op)

nbunch_iter([nbunch])

Returns an iterator over nodes contained in nbunch that are also in the graph.

neighbors(n)

Returns an iterator over successor nodes of n.

nodes_with_selfloops()

number_of_edges([u, v])

Returns the number of edges between two nodes.

number_of_nodes()

Returns the number of nodes in the graph.

number_of_selfloops()

order()

Returns the number of nodes in the graph.

ordered_nodes()

predecessors(n)

Returns an iterator over predecessor nodes of n.

remove_edge(u, v)

Remove the edge between u and v.

remove_edges_from(ebunch)

Remove all edges specified in ebunch.

remove_node(n)

Remove node n.

remove_nodes_from(nodes)

Remove multiple nodes.

reverse([copy])

Returns the reverse of the graph.

selfloop_edges([data, keys, default])

size([weight])

Returns the number of edges or total of all edge weights.

subgraph(nodes)

Returns a SubGraph view of the subgraph induced on nodes.

successors(n)

Returns an iterator over successor nodes of n.

to_circuit()

to_directed([as_view])

Returns a directed representation of the graph.

to_directed_class()

Returns the class to use for empty directed copies.

to_undirected([reciprocal, as_view])

Returns an undirected representation of the digraph.

to_undirected_class()

Returns the class to use for empty undirected copies.

update([edges, nodes])

Update the graph using nodes/edges/graphs as input.

Attributes

adj

Graph adjacency object holding the neighbors of each node.

degree

A DegreeView for the Graph as G.degree or G.degree().

edges

An OutEdgeView of the DiGraph as G.edges or G.edges().

in_degree

An InDegreeView for (node, in_degree) or in_degree for single node.

in_edges

An InEdgeView of the Graph as G.in_edges or G.in_edges().

name

String identifier of the graph.

node

A NodeView of the Graph as G.nodes or G.nodes().

nodes

A NodeView of the Graph as G.nodes or G.nodes().

out_degree

An OutDegreeView for (node, out_degree)

out_edges

An OutEdgeView of the DiGraph as G.edges or G.edges().

pred

Graph adjacency object holding the predecessors of each node.

succ

Graph adjacency object holding the successors of each node.