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.