Wires¶
A class with wire functionality for tensor network applications. |
Wires¶
- class mrmustard.physics.wires.Wires(modes_out_bra=None, modes_in_bra=None, modes_out_ket=None, modes_in_ket=None, classical_out=None, classical_in=None)[source]¶
A class with wire functionality for tensor network applications.
In MrMustard, instances of
CircuitComponenthave aWiresattribute. The wires describe how they connect with the surrounding components in a tensor network picture, where states flow from left to right.CircuitComponents can have wires on the bra and/or on the ket side. Additionally, they may have classical wires. Here are some examples for the types of components available onmrmustard.lab: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│ ╚═════════╝ └───────┘ A ket representing the state of mode ``1`` has only output wires: ╔═════════╗ 1 ┌───────┐ ║ Ket ║─────▶│Ket out│ ╚═════════╝ └───────┘ A measurement acting on mode ``0`` has input wires on the ket side and classical output wires: ┌──────┐ 0 ╔═════════════╗ 0 ┌─────────────┐ │Ket in│─────▶║ Measurement ║─────▶│Classical out│ └──────┘ ╚═════════════╝ └─────────────┘The
Wiresclass can then be used to create subsets of wires:>>> from mrmustard.physics.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
Wiresobject 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
indicesproperty:>>> 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
Wiresclass is to contract the wires of two components. This is done using the@operator. The result is a newWiresobject 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 permutations that standardize the CV and DV variables of the contracted reprs are also returned.
- Parameters:
modes_out_bra (set[int] | None) – The output modes on the bra side.
modes_in_bra (set[int] | None) – The input modes on the bra side.
modes_out_ket (set[int] | None) – The output modes on the ket side.
modes_in_ket (set[int] | None) – The input modes on the ket side.
classical_out (set[int] | None) – The output modes for classical information.
classical_in (set[int] | None) – The input modes for classical information.
- Returns:
A
Wiresobject, and the permutations that standardize the CV and DV variables.
- property adjoint: Wires[source]¶
New
Wiresobject with the adjoint quantum wires (ket becomes bra and vice versa).Note: Wires are not reindexed after this operation.
- property bra: Wires[QuantumWire][source]¶
New
Wiresobject with references to only quantum bra wires. Note that the wires are not copied.
- property classical: Wires[ClassicalWire][source]¶
New
Wiresobject with references to only classical wires. Note that the wires are not copied.
- property dual: Wires[source]¶
New
Wiresobject with dual quantum and classical wires (input becomes output and vice versa).Note: Wires are not reindexed after this operation.
- property input: Wires[source]¶
New
Wiresobject with references to only classical and quantum input wires. Note that the wires are not copied.
- property ket: Wires[QuantumWire][source]¶
New
Wiresobject with references to only quantum ket wires. Note that the wires are not copied.
- property output: Wires[source]¶
New
Wiresobject with references to only classical and quantum output wires. Note that the wires are not copied.
- property quantum: Wires[QuantumWire][source]¶
New
Wiresobject with references to only quantum wires. Note that the wires are not copied.
- property args: tuple[tuple[int, ...], ...]¶
The arguments needed to create a new
Wiresobject with the same wires.
- property classical_wires: set[ClassicalWire]¶
The classical wires.
- property fock_shapes: tuple[int | None, ...]¶
The fock shapes of the wires in standard order.
- property ids: tuple[int, ...]¶
The ids of the wires in standard order.
- property indices: tuple[int, ...]¶
The indices of the wires in standard order.
- property is_ket_like: bool¶
Whether the wires are ket-like.
- property is_dm_like: bool¶
Whether the wires are dm-like.
- property is_unitary_like: bool¶
Whether the wires are unitary-like.
- property modes: set[int]¶
The modes spanned by the wires.
- property quantum_wires: set[QuantumWire]¶
The quantum wires.
- property representations: tuple[ReprEnum, ...]¶
The representations of the wires in standard order.
- classmethod from_wires(quantum=(), classical=(), copy=False)[source]¶
Returns a new Wires object with references to the given wires. If copy is True, the wires are copied, otherwise they are referenced. Does not reindex the wires.
- Parameters:
quantum (Iterable[QuantumWire])
classical (Iterable[ClassicalWire])
copy (bool)
- Return type:
- contracted_indices(other)[source]¶
Returns the indices (in standard order) being contracted between self and other when calling matmul.
- Parameters:
other (Wires) – another Wires object
- Returns:
The indices being contracted between self and other in standard order.
- Return type:
tuple[tuple[int, …], tuple[int, …]]
- contracted_labels(other)[source]¶
Returns the integer labels of the contracted wires, such that contracted wires have the same label. The output labels are sorted in standard order.
- Parameters:
other (Wires) – another Wires object
- Return type:
tuple[list[int], list[int], list[int]]
- copy(new_ids=False)[source]¶
Returns a deep copy of this Wires object.
- Parameters:
new_ids (bool)
- Return type:
- overlap(other)[source]¶
Returns the modes that overlap between self and other on the bra, ket, and classical wires.
- Parameters:
other (Wires) – Another
Wiresobject.- Returns:
A tuple of sets of modes that overlap between self and other on the bra, ket, and classical wires respectively.
- Return type:
tuple[set[int], set[int], set[int]]