mm.lab_dev.wires.Wires¶
- class mrmustard.lab_dev.wires.Wires(modes_out_bra=None, modes_in_bra=None, modes_out_ket=None, modes_in_ket=None)[source]¶
Bases:
object
A class with wire functionality for tensor network applications.
In MrMustard, instances of
CircuitComponent
have aWires
attribute. The wires describe how they connect with the surrounding components in a tensor network picture, where states flow from left to right.CircuitComponent
s can have wires on the bra and/or on the ket side. Here are some examples for the types of components available onmrmustard.lab_dev
:A channel acting on mode ``1`` has input and output wires on both ket and bra sides: ┌──────┐ 1 ╔═════════╗ 1 ┌───────┐ │Bra in│─────▶║ ║─────▶│Bra out│ └──────┘ ║ Channel ║ └───────┘ ┌──────┐ 1 ║ ║ 1 ┌───────┐ │Ket in│─────▶║ ║─────▶│Ket out│ └──────┘ ╚═════════╝ └───────┘ A unitary acting on mode ``2`` has input and output wires on the ket side: ┌──────┐ 2 ╔═════════╗ 2 ┌───────┐ │Ket in│─────▶║ Unitary ║─────▶│Ket out│ └──────┘ ╚═════════╝ └───────┘ A density matrix representing the state of mode ``0`` has only output wires: ╔═════════╗ 0 ┌───────┐ ║ ║─────▶│Bra out│ ║ Density ║ └───────┘ ║ Matrix ║ 0 ┌───────┐ ║ ║─────▶│Ket out│ ╚═════════╝ └───────┘ Also a ket representing the state of mode ``1`` has only output wires: ╔═════════╗ 1 ┌───────┐ ║ Ket ║─────▶│Ket out│ ╚═════════╝ └───────┘
The
Wires
class can then be used to create subsets of wires:>>> from mrmustard.lab_dev.wires import Wires >>> modes_out_bra={0, 1} >>> modes_in_bra={1, 2} >>> modes_out_ket={0, 13} >>> modes_in_ket={1, 2, 13} >>> w = Wires(modes_out_bra, modes_in_bra, modes_out_ket, modes_in_ket) >>> # all the modes >>> modes = w.modes >>> assert w.modes == {0, 1, 2, 13} >>> # input/output modes >>> assert w.input.modes == {1, 2, 13} >>> assert w.output.modes == {0, 1, 13} >>> # get ket/bra modes >>> assert w.ket.modes == {0, 1, 2, 13} >>> assert w.bra.modes == {0, 1, 2} >>> # combined subsets >>> assert w.output.ket.modes == {0, 13} >>> assert w.input.bra.modes == {1, 2}
Here’s a diagram of the original
Wires
object in the example above, with the indices of the wires (the number in parenthesis) given in the “standard” order (bra_out
,bra_in
,ket_out
,ket_in
, and the modes in sorted increasing order):╔═════════╗ 1 (2) ─────▶ ║ ║─────▶ 0 (0) 2 (3) ─────▶ ║ ║─────▶ 1 (1) ║ ║ ║ ``Wires`` ║ 1 (6) ─────▶ ║ ║ 2 (7) ─────▶ ║ ║─────▶ 0 (4) 13 (8) ─────▶ ║ ║─────▶ 13 (5) ╚═════════╝
To access the index of a subset of wires in standard order we can use the
indices
property:>>> assert w.indices == (0,1,2,3,4,5,6,7,8) >>> assert w.input.indices == (2,3,6,7,8)
Another important application of the
Wires
class is to contract the wires of two components. This is done using the@
operator. The result is a newWires
object that combines the wires of the two components. Here’s an example of a contraction of a single-mode density matrix going into a single-mode channel:>>> rho = Wires(modes_out_bra={0}, modes_in_bra={0}) >>> Phi = Wires(modes_out_bra={0}, modes_in_bra={0}, modes_out_ket={0}, modes_in_ket={0}) >>> rho_out, perm = rho @ Phi >>> assert rho_out.modes == {0}
Here’s a diagram of the result of the contraction:
╔═══════╗ ╔═══════╗ ║ ║─────▶║ ║─────▶ 0 ║ rho ║ ║ Phi ║ ║ ║─────▶║ ║─────▶ 0 ╚═══════╝ ╚═══════╝
The permutation that takes the contracted representations to the standard order is also returned.
- Parameters:
modes_out_bra (
Optional
[set
[int
]]) – The output modes on the bra side.modes_in_bra (
Optional
[set
[int
]]) – The input modes on the bra side.modes_out_ket (
Optional
[set
[int
]]) – The output modes on the ket side.modes_in_ket (
Optional
[set
[int
]]) – The input modes on the ket side.
Attributes
New
Wires
object obtained by swapping ket and bra wires.New
Wires
object without ket wires.New
Wires
object obtained by swapping input and output wires.A numerical identifier for this
Wires
object.A list of numerical identifier for the wires in this
Wires
object, in the standard order.A list of dictionary mapping modes to
ids
, one for each of the subsets (output.bra
,input.bra
,output.ket
, andinput.ket
).A list of dictionary mapping modes to indices, one for each of the subsets (
output.bra
,input.bra
,output.ket
, andinput.ket
).The array of indices of this
Wires
in the standard order.New
Wires
object without output wires.New
Wires
object without bra wires.The modes spanned by the wires.
The parent wire, if any.
New
Wires
object without input wires.The sorted arguments.
- adjoint¶
New
Wires
object obtained by swapping ket and bra wires.
- bra¶
New
Wires
object without ket wires.
- dual¶
New
Wires
object obtained by swapping input and output wires.
- id¶
A numerical identifier for this
Wires
object.The
id
are random and unique, and are preserved when taking subsets.
- ids¶
A list of numerical identifier for the wires in this
Wires
object, in the standard order.The
ids
are derived incrementally from theid
and are unique.>>> w = Wires(modes_in_ket = {0,1}, modes_out_ket = {0,1}) >>> id = w.id >>> ids = w.ids >>> assert ids == [id, id+1, id+2, id+3]
- ids_dicts¶
A list of dictionary mapping modes to
ids
, one for each of the subsets (output.bra
,input.bra
,output.ket
, andinput.ket
).If subsets are taken,
ids_dicts
refers to the parent object rather than to the child.
- index_dicts¶
A list of dictionary mapping modes to indices, one for each of the subsets (
output.bra
,input.bra
,output.ket
, andinput.ket
).If subsets are taken,
index_dicts
refers to the parent object rather than to the child.
- indices¶
The array of indices of this
Wires
in the standard order. When a subset is selected (e.g..ket
), it doesn’t include wires that do not belong to the subset, but it still counts them because indices refer to the original modes.>>> w = Wires(modes_in_ket = {0,1}, modes_out_ket = {0,1}) >>> assert w.indices == (0,1,2,3) >>> assert w.input.indices == (2,3)
- input¶
New
Wires
object without output wires.
- ket¶
New
Wires
object without bra wires.
- modes¶
The modes spanned by the wires.
- original¶
The parent wire, if any.
- output¶
New
Wires
object without input wires.
- sorted_args¶
The sorted arguments. Allows to sort them only once.