tfq.convert_to_tensor
Stay organized with collections
Save and categorize content based on your preferences.
Convert lists of tfq supported primitives to tensor representations.
tfq.convert_to_tensor(
items_to_convert, deterministic_proto_serialize=False
)
Used in the notebooks
Recursively convert a nested lists of cirq.PauliSum
or cirq.Circuit
objects to a tf.Tensor
representation. Note that cirq serialization only
supports cirq.GridQubit
s so we also require that input circuits and
pauli sums are defined only on cirq.GridQubit
s.
my_qubits = cirq.GridQubit.rect(1, 2)
my_circuits = [cirq.Circuit(cirq.X(my_qubits[0])),
cirq.Circuit(cirq.Z(my_qubits[0]))
]
tensor_input = tfq.convert_to_tensor(my_circuits)
# Now tensor_input can be used as model input etc.
same_circuits = tfq.from_tensor(tensor_input)
# same_circuits now holds cirq.Circuit objects once more.
same_circuits
[cirq.Circuit([
cirq.Moment(operations=[
cirq.X.on(cirq.GridQubit(0, 0)),
]),
])
cirq.Circuit([
cirq.Moment(operations=[
cirq.Z.on(cirq.GridQubit(0, 0)),
]),
])]
Args |
items_to_convert
|
Python list or nested list of cirq.Circuit
or cirq.Paulisum objects. Must be recangular.
|
deterministic_proto_serialize
|
Whether to use a deterministic
serialization when calling SerializeToString().
|
Returns |
A tf.Tensor that represents the input items.
|
Raises |
TypeError
|
In case of invalid arguments provided in items_to_convert .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-05-17 UTC.
[null,null,["Last updated 2024-05-17 UTC."],[],[],null,["# tfq.convert_to_tensor\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/quantum/blob/v0.7.3/tensorflow_quantum/python/util.py#L263-L333) |\n\nConvert lists of tfq supported primitives to tensor representations. \n\n tfq.convert_to_tensor(\n items_to_convert, deterministic_proto_serialize=False\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Hello, many worlds](https://www.tensorflow.org/quantum/tutorials/hello_many_worlds) - [Quantum Convolutional Neural Network](https://www.tensorflow.org/quantum/tutorials/qcnn) - [Calculate gradients](https://www.tensorflow.org/quantum/tutorials/gradients) - [MNIST classification](https://www.tensorflow.org/quantum/tutorials/mnist) - [Quantum data](https://www.tensorflow.org/quantum/tutorials/quantum_data) |\n\nRecursively convert a nested lists of [`cirq.PauliSum`](https://quantumai.google/reference/python/cirq/PauliSum) or [`cirq.Circuit`](https://quantumai.google/reference/python/cirq/Circuit)\nobjects to a [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) representation. Note that cirq serialization only\nsupports [`cirq.GridQubit`](https://quantumai.google/reference/python/cirq/GridQubit)s so we also require that input circuits and\npauli sums are defined only on [`cirq.GridQubit`](https://quantumai.google/reference/python/cirq/GridQubit)s. \n\n my_qubits = cirq.GridQubit.rect(1, 2)\n my_circuits = [cirq.Circuit(cirq.X(my_qubits[0])),\n cirq.Circuit(cirq.Z(my_qubits[0]))\n ]\n tensor_input = tfq.convert_to_tensor(my_circuits)\n # Now tensor_input can be used as model input etc.\n same_circuits = tfq.from_tensor(tensor_input)\n # same_circuits now holds cirq.Circuit objects once more.\n same_circuits\n [cirq.Circuit([\n cirq.Moment(operations=[\n cirq.X.on(cirq.GridQubit(0, 0)),\n ]),\n ])\n cirq.Circuit([\n cirq.Moment(operations=[\n cirq.Z.on(cirq.GridQubit(0, 0)),\n ]),\n ])]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `items_to_convert` | Python `list` or nested `list` of [`cirq.Circuit`](https://quantumai.google/reference/python/cirq/Circuit) or `cirq.Paulisum` objects. Must be recangular. |\n| `deterministic_proto_serialize` | Whether to use a deterministic serialization when calling SerializeToString(). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) that represents the input items. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------------------------------------------|\n| `TypeError` | In case of invalid arguments provided in `items_to_convert`. |\n\n\u003cbr /\u003e"]]