Computational Graph

ComputationalGraph

A computational graph is a directed graph that represents the computation of a quantum circuit.

ComputationalGraph

class mrmustard.lab.computational_graph.ComputationalGraph[source]

A computational graph is a directed graph that represents the computation of a quantum circuit. Nodes are comprised of components and edges are wires between nodes.

property ansatz_dict: dict[type, AnsatzFactory]

The ansatz dictionary for the computational graph.

property graph: PyDiGraph

The underlying rustworkx graph.

property mm_einsum_str: str

The mm_einsum string for the computational graph.

Raises:

ValueError – If the computational graph contains more than 26 unique indices.

property name_to_idx: dict[str, int]

A dictionary mapping node names to their indices in the graph.

property uncontracted_wires: dict[str, list[QuantumWire | ClassicalWire]]

A dictionary mapping node names to their uncontracted wires.

add_component(component, name)[source]

Add a component to the computational graph.

Parameters:
  • component (CircuitComponent) – The component to add.

  • name (str) – The name of the component.

Returns:

The index of the added component.

Return type:

int

add_components(components, names)[source]

Add components to the computational graph from a list of components and names.

Parameters:
  • components (list[CircuitComponent]) – A list of circuit components.

  • names (list[str]) – A list of names for the components.

Returns:

A list of indices of the added components.

Raises:

ValueError – If the number of components and names are not the same.

Return type:

list[int]

add_wire(component_a, component_b)[source]

Add a wire between two nodes in the graph. Nodes must be of the form component_name[wire_spec].

wire_spec is a string consisting of three characters:
  • i / o for input / output.

  • k / b for ket / bra.

  • 0 / 1 / 2, etc. for mode.

For example, ib0 means an input bra on mode 0 whereas ok1 means an output ket on mode 1.

Parameters:
  • component_a (str) – The name and wire of the first component.

  • component_b (str) – The name and wire of the second component.

Returns:

None

Raises:

ValueError – If a wire is not supported.

Return type:

None

add_wires(wires)[source]

Add wires between components in the graph from a list of wire tuples. See add_wire() for more details.

Parameters:

wires (list[tuple[str, str]]) – A list of wire tuples.

Returns:

None

Return type:

None

fock_config(representation_config=None, verbose=False)[source]

Returns an optimized Fock configuration given a representation configuration by using a recursive optimization strategy.

Parameters:
  • representation_config (dict[str, ReprEnum] | None) – A representation configuration.

  • verbose (bool) – Whether to print the progress.

Returns:

A dictionary mapping wire labels to their respective Fock dimensions.

Return type:

dict[int, int]

representation_config()[source]

Returns a default representation configuration for the computational graph.

Currently, this is the representation of the component instance (selected via representations()) however optimizations are possible in the future.

Returns:

A dictionary mapping component names to their representations.

Return type:

dict[str, ReprEnum]

run(representation_config=None, fock_config=None, path_config=None)[source]

Runs the computational graph.

Parameters:
  • representation_config (dict[str, ReprEnum] | None) – A representation configuration.

  • fock_config (dict[int, int] | None) – A Fock configuration.

  • path_config (list[tuple[int, int]] | None) – A contraction path configuration.

Returns:

The result of the computational graph.

Raises:

RuntimeError – If the graph is underspecified.

Return type:

Scalar

to_component(representation_config=None, fock_config=None, path_config=None)[source]

Converts the computational graph to a CircuitComponent.

Parameters:
  • representation_config (dict[str, ReprEnum] | None) – A representation configuration.

  • fock_config (dict[int, int] | None) – A Fock configuration.

  • path_config (list[tuple[int, int]] | None) – A contraction path configuration.

Returns:

A CircuitComponent representing the computational graph in its current state.

Return type:

CircuitComponent