# cirq.LinearCombinationOfGates

Represents linear operator defined by a linear combination of gates.

Inherits From: `LinearDict`

Suppose G1, G2, ..., Gn are gates and b1, b2, ..., bn are complex numbers. Then

``````LinearCombinationOfGates({G1: b1, G2: b2, ..., Gn: bn})
``````

represents the linear operator

``````A = b1 G1 + b2 G2 + ... + bn Gn
``````

Note that A may not be unitary or even normal.

Rather than creating LinearCombinationOfGates instance explicitly, one may use overloaded arithmetic operators. For example,

``````cirq.LinearCombinationOfGates({cirq.X: 2, cirq.Z: -2})
``````

is equivalent to

``````2 * cirq.X - 2 * cirq.Z
``````

`terms` Mapping of gates to coefficients in the linear combination being initialized.

## Methods

### `clean`

View source

Remove terms with coefficients of absolute value atol or less.

D.clear() -> None. Remove all items from D.

View source

View source

### `get`

View source

D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

### `items`

View source

D.items() -> a set-like object providing a view on D's items

### `keys`

View source

D.keys() -> a set-like object providing a view on D's keys

### `matrix`

View source

Reconstructs matrix of self using unitaries of underlying gates.

Raises
`ValueError` If the number of qubits has not been specified.

### `num_qubits`

View source

Returns number of qubits in the domain if known, None if unknown.

### `pop`

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised.

### `popitem`

D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.

### `setdefault`

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

### `update`

View source

D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

### `values`

View source

D.values() -> an object providing a view on D's values

View source

View source

View source

### `__eq__`

View source

Checks whether two linear combinations are exactly equal.

Presence or absence of terms with coefficients exactly equal to zero does not affect outcome.

Not appropriate for most practical purposes due to sensitivity to numerical error in floating point coefficients. Use cirq.approx_eq() instead.

View source

View source

View source

View source

### `__ne__`

View source

Checks whether two linear combinations are not exactly equal.

See eq().

View source

View source

View source

View source

### `__truediv__`

View source

TSelf Instance of `typing.TypeVar`

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