qp.math.is_tracer

is_tracer(tensor, like=None)[source]

Return whether the tensor is a JAX tracer without a concrete value.

Such tracers commonly occur when tracing functions decorated with @jax.jit. This function does not identify PennyLane shape/type descriptors such as qp.typing.Float[3] or qp.typing.Wire[3]. Tracers with a concrete value also return False.

Note

Currently Autograd tensors and Torch tensors will always return False. This is because:

  • Autograd does not provide JIT compilation, and

  • @torch.jit.script is not currently compatible with QNodes.

Parameters:
  • tensor (tensor_like) – input tensor

  • like (str) – The name of the interface. Will be determined automatically if not provided.

Returns:

whether the tensor is a tracer without a concrete value

Return type:

bool

Example

Consider the following JAX function:

import jax
from jax import numpy as jnp

def function(x):
    print("Value:", x)
    print("Tracer:", qp.math.is_tracer(x))
    return jnp.sum(x ** 2)

When we execute it, we see that the tensor is not abstract; it has known value:

>>> x = jnp.array([0.5, 0.1])
>>> function(x)
Value: [0.5, 0.1]
Tracer: False
Array(0.26, dtype=float32)

However, if we use the @jax.jit decorator, the tensor will now be abstract:

>>> x = jnp.array([0.5, 0.1])
>>> jax.jit(function)(x)
Value: Traced<ShapedArray(float32[2])>with<DynamicJaxprTrace(level=0/1)>
Tracer: True
Array(0.26, dtype=float32)

Note that JAX uses an abstract shaped array, so although we won’t be able to include conditionals within our function that depend on the value of the tensor, we can include conditionals that depend on the shape of the tensor.