qml.math.diag

diag(values, k=0, like=None)[source]

Construct a diagonal tensor from a list of scalars.

Parameters
  • values (tensor_like or Sequence[scalar]) – sequence of numeric values that make up the diagonal

  • k (int) – The diagonal in question. k=0 corresponds to the main diagonal. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.

Returns

the 2D diagonal tensor

Return type

tensor_like

Example

>>> x = [1., 2., tf.Variable(3.)]
>>> qml.math.diag(x)
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 0., 0.],
       [0., 2., 0.],
       [0., 0., 3.]], dtype=float32)>
>>> y = tf.Variable([0.65, 0.2, 0.1])
>>> qml.math.diag(y, k=-1)
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[0.  , 0.  , 0.  , 0.  ],
       [0.65, 0.  , 0.  , 0.  ],
       [0.  , 0.2 , 0.  , 0.  ],
       [0.  , 0.  , 0.1 , 0.  ]], dtype=float32)>
>>> z = torch.tensor([0.1, 0.2])
>>> qml.math.diag(z, k=1)
tensor([[0.0000, 0.1000, 0.0000],
        [0.0000, 0.0000, 0.2000],
        [0.0000, 0.0000, 0.0000]])

Contents

Using PennyLane

Development

API

Internals