cirq.LinearCombinationOfGates  |  Cirq  |  Google Quantum AI (original) (raw)

Represents linear operator defined by a linear combination of gates.

Inherits From: LinearDict

cirq.LinearCombinationOfGates(
    terms: Mapping[raw_types.Gate, 'cirq.TParamValComplex']
) -> None

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
Args
terms Mapping of gates to coefficients in the linear combination being initialized.

Methods

clean

View source

clean(
    *, atol: float = 1e-09
) -> Self

Remove terms with coefficients of absolute value atol or less.

clear

clear()

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

copy

View source

copy() -> Self

fromkeys

View source

@classmethod fromkeys( vectors, coefficient=0 )

get

View source

get(
    vector, default=0
)

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

items

View source

items() -> ItemsView[TVector, 'cirq.TParamValComplex']

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

keys

View source

keys() -> KeysView[TVector]

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

matrix

View source

matrix() -> np.ndarray

Reconstructs matrix of self using unitaries of underlying gates.

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

num_qubits

View source

num_qubits() -> Optional[int]

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

pop

pop(
    key, default=__marker
)

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

popitem()

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

setdefault

setdefault(
    key, default=None
)

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

update

View source

update(
    *args, **kwargs
)

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

values() -> ValuesView['cirq.TParamValComplex']

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

__add__

View source

__add__(
    other: Union[raw_types.Gate, 'LinearCombinationOfGates']
) -> 'LinearCombinationOfGates'

__bool__

View source

__bool__() -> bool

__contains__

View source

__contains__(
    vector: Any
) -> bool

__eq__

View source

__eq__(
    other: Any
) -> bool

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.

__getitem__

View source

__getitem__(
    vector: TVector
) -> 'cirq.TParamValComplex'

__iter__

View source

__iter__() -> Iterator[TVector]

__len__

View source

__len__() -> int

__mul__

View source

__mul__(
    a: 'cirq.TParamValComplex'
) -> Self

__ne__

View source

__ne__(
    other: Any
) -> bool

Checks whether two linear combinations are not exactly equal.

See eq().

__neg__

View source

__neg__() -> Self

__pow__

View source

__pow__(
    exponent: int
) -> 'LinearCombinationOfGates'

__rmul__

View source

__rmul__(
    a: 'cirq.TParamValComplex'
) -> Self

__sub__

View source

__sub__(
    other: Union[raw_types.Gate, 'LinearCombinationOfGates']
) -> 'LinearCombinationOfGates'

__truediv__

View source

__truediv__(
    a: 'cirq.TParamValComplex'
) -> Self