mm.lab_dev.circuits.Circuit

class mrmustard.lab_dev.circuits.Circuit(components=None)[source]

Bases: object

A quantum circuit.

Quantum circuits store a list of ``CircuitComponent``s.

>>> from mrmustard.lab_dev import BSgate, Sgate, Vacuum, Circuit

>>> vac = Vacuum([0, 1, 2])
>>> s01 = Sgate([0, 1], r=[0.1, 0.2])
>>> bs01 = BSgate([0, 1])
>>> bs12 = BSgate([1, 2])

>>> components = [vac, s01, bs01, bs12]
>>> circ = Circuit(components)
>>> assert circ.components == components

New components (or entire circuits) can be appended using the >> operator.

>>> from mrmustard.lab_dev import BSgate, Sgate, Vacuum, Circuit

>>> vac = Vacuum([0, 1, 2])
>>> s01 = Sgate([0, 1], r=[0.1, 0.2])
>>> bs01 = BSgate([0, 1])
>>> bs12 = BSgate([1, 2])

>>> circ1 = Circuit() >> vac >> s01
>>> circ2 = Circuit([bs01]) >> bs12
>>> assert circ1 >> circ2 == Circuit([vac, s01, bs01, bs12])
Parameters:

components (Optional[Sequence[CircuitComponent]]) – A list of circuit components.

components

The components in this circuit.

path

A list describing the desired contraction path followed by the Simulator.

components

The components in this circuit.

path

A list describing the desired contraction path followed by the Simulator.

lookup_path()

An auxiliary function that helps building the contraction path for this circuit.

make_path([strategy])

Automatically generates a path for this circuit.

validate_path(path)

A convenience function to check whether a given contraction path is valid for this circuit.

lookup_path()[source]

An auxiliary function that helps building the contraction path for this circuit.

Shows the remaining components and the corresponding contraction indices.

>>> from mrmustard.lab_dev import BSgate, Sgate, Vacuum, Circuit

>>> vac = Vacuum([0, 1, 2])
>>> s01 = Sgate([0, 1], r=[0.1, 0.2])
>>> bs01 = BSgate([0, 1])
>>> bs12 = BSgate([1, 2])

>>> circ = Circuit([vac, s01, bs01, bs12])

>>> # ``circ`` has no path: all the components are available, and indexed
>>> # as they appear in the list of components
>>> circ.lookup_path()

→ index: 0
mode 0:     ◖Vac◗
mode 1:     ◖Vac◗
mode 2:     ◖Vac◗


→ index: 1
mode 0:   ──Sgate(0.1,0.0)
mode 1:   ──Sgate(0.2,0.0)


→ index: 2
mode 0:   ──╭•──────────────
mode 1:   ──╰BSgate(0.0,0.0)


→ index: 3
mode 1:   ──╭•──────────────
mode 2:   ──╰BSgate(0.0,0.0)




>>> # start building the path
>>> circ.path = [(0, 1)]
>>> circ.lookup_path()

→ index: 0
mode 0:     ◖Vac◗──Sgate(0.1,0.0)
mode 1:     ◖Vac◗──Sgate(0.2,0.0)
mode 2:     ◖Vac◗────────────────


→ index: 2
mode 0:   ──╭•──────────────
mode 1:   ──╰BSgate(0.0,0.0)


→ index: 3
mode 1:   ──╭•──────────────
mode 2:   ──╰BSgate(0.0,0.0)




>>> circ.path = [(0, 1), (2, 3)]
>>> circ.lookup_path()

→ index: 0
mode 0:     ◖Vac◗──Sgate(0.1,0.0)
mode 1:     ◖Vac◗──Sgate(0.2,0.0)
mode 2:     ◖Vac◗────────────────


→ index: 2
mode 0:   ──╭•────────────────────────────────
mode 1:   ──╰BSgate(0.0,0.0)──╭•──────────────
mode 2:                     ──╰BSgate(0.0,0.0)




>>> circ.path = [(0, 1), (2, 3), (0, 2)]
>>> circ.lookup_path()

→ index: 0
mode 0:     ◖Vac◗──Sgate(0.1,0.0)──╭•────────────────────────────────
mode 1:     ◖Vac◗──Sgate(0.2,0.0)──╰BSgate(0.0,0.0)──╭•──────────────
mode 2:     ◖Vac◗────────────────────────────────────╰BSgate(0.0,0.0)


Raises:

ValueError – If circuit.path contains invalid contractions.

Return type:

None

make_path(strategy='l2r')[source]

Automatically generates a path for this circuit.

The available strategies are:
  • l2r: The two left-most components are contracted together, then the

    resulting component is contracted with the third one from the left, et cetera.

  • r2l: The two right-most components are contracted together, then the

    resulting component is contracted with the third one from the right, et cetera.

Parameters:

strategy (str) – The strategy used to generate the path.

Return type:

None

validate_path(path)[source]

A convenience function to check whether a given contraction path is valid for this circuit.

Uses the wires’ ids to understand what pairs of wires would be contracted, if the simulation was carried from left to right. Next, it checks whether path is an equivalent contraction path, meaning that it instructs to contract the same wires as a l2r path.

Parameters:

path – A candidate contraction path.

Raises:

ValueError – If the given path is not equivalent to a left-to-right path.

Return type:

None