tensorcircuit.interfaces.tensorflow#

Interface wraps quantum function as a tensorflow function

tensorcircuit.interfaces.tensorflow.tensorflow_interface(fun: Callable[[...], Any], ydtype: Any, jit: bool = False, enable_dlpack: bool = False) → Callable[[...], Any][source]#

Wrap a quantum function on different ML backend with a tensorflow interface.

Example

K = tc.set_backend("jax")


def f(params):
    c = tc.Circuit(1)
    c.rx(0, theta=params[0])
    c.ry(0, theta=params[1])
    return K.real(c.expectation([tc.gates.z(), [0]]))


f = tc.interfaces.tf_interface(f, ydtype=tf.float32, jit=True)

tfb = tc.get_backend("tensorflow")
grads = tfb.jit(tfb.grad(f))(tfb.ones([2]))
Parameters
  • fun (Callable[..., Any]) – The quantum function with tensor in and tensor out

  • ydtype (Any) – output tf dtype or in str

  • jit (bool, optional) – whether to jit fun, defaults to False

  • enable_dlpack (bool, optional) – whether transform tensor backend via dlpack, defaults to False

Returns

The same quantum function but now with torch tensor in and torch tensor out while AD is also supported

Return type

Callable[…, Any]

tensorcircuit.interfaces.tensorflow.tf_dtype(dtype: str) → Any[source]#
tensorcircuit.interfaces.tensorflow.tf_interface(fun: Callable[[...], Any], ydtype: Any, jit: bool = False, enable_dlpack: bool = False) → Callable[[...], Any]#

Wrap a quantum function on different ML backend with a tensorflow interface.

Example

K = tc.set_backend("jax")


def f(params):
    c = tc.Circuit(1)
    c.rx(0, theta=params[0])
    c.ry(0, theta=params[1])
    return K.real(c.expectation([tc.gates.z(), [0]]))


f = tc.interfaces.tf_interface(f, ydtype=tf.float32, jit=True)

tfb = tc.get_backend("tensorflow")
grads = tfb.jit(tfb.grad(f))(tfb.ones([2]))
Parameters
  • fun (Callable[..., Any]) – The quantum function with tensor in and tensor out

  • ydtype (Any) – output tf dtype or in str

  • jit (bool, optional) – whether to jit fun, defaults to False

  • enable_dlpack (bool, optional) – whether transform tensor backend via dlpack, defaults to False

Returns

The same quantum function but now with torch tensor in and torch tensor out while AD is also supported

Return type

Callable[…, Any]

tensorcircuit.interfaces.tensorflow.tf_wrapper(fun: Callable[[...], Any], enable_dlpack: bool = False) → Callable[[...], Any][source]#