# openfermion.ops.BinaryCode

The BinaryCode class provides a representation of an encoding-decoding pair for binary vectors of different lengths, where the decoding is allowed to be non-linear.

### Used in the notebooks

Used in the tutorials

As the occupation number of fermionic mode is effectively binary, a length-N vector (v) of binary number can be utilized to describe a configuration of a many-body fermionic state on N modes. An n-qubit product state configuration |w0> |w1> |w2> ... |wn-1>, on the other hand is described by a length-n binary vector w=(w0, w1, ..., wn-1). To map a subset of N-Orbital Fermion states to n-qubit states we define a binary code, which consists of a (here: linear) encoding (e) and a (non-linear) decoding (d), such that for every v from that subset, w = e(v) is a length-n binary vector with d(w) = v. This can be used to save qubits given a Hamiltonian that dictates such a subset, otherwise n=N.

Two binary codes (e,d) and (e',d') can construct a third code (e",d") by two possible operations:

Concatenation: (e",d") = (e,d) * (e',d') which means e": v" -> e'( e(v") ) and d": w" -> d( d'(w") ) where n" = n' and N" = N, with n = N' as necessary condition.

Appendage: (e",d") = (e,d) + (e',d') which means e": (v + v') -> e(v) + e'(v') and d": (w + w') -> d(w) + d'( w') where the addition is to be understood as appending two vectors together, so N" = N' + N and n" = n + n'.

Appending codes is particularly useful when considering segment codes or segmented transforms.

A BinaryCode-instance is initialized by BinaryCode(A,d), given the encoding (e) as n x N array or matrix-like nested lists A, such that e(v) = (A v) mod 2. The decoding d is an array or a list input of length N, which has entries either of type BinaryPolynomial, or of valid type for an input of the BinaryPolynomial-constructor.

The signs + and *, += and *= are overloaded to implement concatenation and appendage on BinaryCode-objects.

`encoding` `np.ndarray or list`

nested lists or binary 2D-array

`decoding` `array or list`

list of BinaryPolynomial (list or str).

`TypeError` non-list, array like encoding or decoding, unsuitable BinaryPolynomial generators,
`BinaryCodeError` in case of decoder/encoder size mismatch or decoder size, qubits indexed mismatch

`decoder` `list`

list of BinaryPolynomial: Outputs the decoding functions as components.

`encoder` `scipy.sparse.csc_matrix`

Outputs A, the linear matrix that implements the encoding function.

`n_modes` `int`

Outputs the number of modes.

`n_qubits` `int`

Outputs the number of qubits.

## Methods

### `__add__`

View source

Appends two binary codes via addition +.

Args
`appendix` `BinaryCode`

The code to append to the present one.

Returns (BinaryCode): global binary code

### `__mul__`

View source

Concatenation of two codes or appendage the same code factor times in case of integer factor.

Args
`factor` `int or BinaryCode`

the BinaryCode to concatenate. In case of int, it will append the code to itself factor times.

Returns (BinaryCode): segmented or concatenated code

### `__rmul__`

View source

Appending the same code factor times.

Args
`factor` `int`

integer defining number of appendages.

Returns (BinaryCode): Segmented code.

Raises
`TypeError` factor must be an integer

[{ "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" }]