# openfermion.circuits.swap_network

Apply operations to pairs of qubits or modes using a swap network.

This is used for applying operations between arbitrary pairs of qubits or fermionic modes using only nearest-neighbor interactions on a linear array of qubits. It works by reversing the order of qubits or modes with a sequence of swap gates and applying an operation when the relevant qubits or modes become adjacent. For fermionic modes, this assumes the Jordan-Wigner Transform.

## Examples

#### Input:

.. testcode::

``````import cirq
from openfermion import swap_network

qubits = cirq.LineQubit.range(4)
circuit = cirq.Circuit(swap_network(qubits))
print(circuit)
``````

#### Output:

.. testoutput::

``````0: ───×───────×───────
│       │
1: ───×───×───×───×───
│       │
2: ───×───×───×───×───
│       │
3: ───×───────×───────
``````

#### Input:

.. testcode::

``````circuit = cirq.Circuit(swap_network(qubits, offset=True))
print(circuit)
``````

#### Output:

.. testoutput::

``````0: ───────×───────×───
│       │
1: ───×───×───×───×───
│       │
2: ───×───×───×───×───
│       │
3: ───────×───────×───
``````

#### Input:

.. testcode::

``````circuit = cirq.Circuit(
swap_network(
qubits,
lambda p, q, a, b: cirq.ISWAP(a, b)**-1 if abs(p - q) == 1
else cirq.CZ(a, b),
fermionic=True))
print(circuit)
``````

#### Output:

.. testoutput::

``````0: ───iSwap──────×ᶠ────────────@───×ᶠ───────────────────
│          │             │   │
1: ───iSwap^-1───×ᶠ───@───×ᶠ───@───×ᶠ───iSwap──────×ᶠ───
│   │             │          │
2: ───iSwap──────×ᶠ───@───×ᶠ───@───×ᶠ───iSwap^-1───×ᶠ───
│          │             │   │
3: ───iSwap^-1───×ᶠ────────────@───×ᶠ───────────────────
``````

`qubits` The qubits sorted so that the j-th qubit in the Sequence represents the j-th qubit or fermionic mode.
`operation` Returns extra interactions to perform between qubits/modes as they are swapped past each other. A call to this function takes the form `operation(p, q, p_qubit, q_qubit)` where p and q are indices representing either qubits or fermionic modes, and p_qubit and q_qubit are the qubits which are currently storing those modes.
`fermionic` If True, use fermionic swaps under the JWT (that is, swap fermionic modes instead of qubits). If False, use normal qubit swaps.
`offset` If True, then qubit 0 will participate in odd-numbered layers instead of even-numbered layers.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]
{"lastModified": "Last updated 2024-04-26 UTC."}