Skip to content

Commit cbe2232

Browse files
Merge branch 'beta' into cloud
2 parents 09f800d + cf4209d commit cbe2232

File tree

8 files changed

+740
-60
lines changed

8 files changed

+740
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Added
66

7-
- Add `tc.about()` to print related software versions and configs.
7+
- Add `tc.about()` to print related software versions and configs
88

99
- Torch support is updraded to 2.0, and now support native vmap and native functional grad, and thus `vvag`. Still jit support is conflict with these functional transformations and be turned off by default
1010

docs/source/quickstart.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ and the other part is implemented in `TensorCircuit package <modules.html#module
318318
'expm',
319319
'eye',
320320
'from_dlpack',
321+
'g',
321322
'gather1d',
322323
'get_random_state',
323324
'gmres',
@@ -345,6 +346,7 @@ and the other part is implemented in `TensorCircuit package <modules.html#module
345346
'max',
346347
'mean',
347348
'min',
349+
'minor',
348350
'mod',
349351
'multiply',
350352
'name',
@@ -353,6 +355,7 @@ and the other part is implemented in `TensorCircuit package <modules.html#module
353355
'one_hot',
354356
'onehot',
355357
'ones',
358+
'optimizer',
356359
'outer_product',
357360
'pivot',
358361
'power',
@@ -419,6 +422,7 @@ and the other part is implemented in `TensorCircuit package <modules.html#module
419422
'vvag',
420423
'zeros']
421424
425+
422426
423427
Switch the Dtype
424428
--------------------
@@ -687,6 +691,20 @@ As we have mentioned in the backend section, the PyTorch backend may lack advanc
687691
688692
For a GPU/CPU, torch/tensorflow, quantum/classical hybrid machine learning pipeline enabled by tensorcircuit, see `example script <https://github.com/tencent-quantum-lab/tensorcircuit/blob/master/examples/hybrid_gpu_pipeline.py>`__.
689693
694+
There is also a more flexible torch interface that support static non-tensor inputs as keyword arguments, which can be utilized as below:
695+
696+
.. code-block:: python
697+
698+
def f(a, i):
699+
s = 0.
700+
for _ in range(i):
701+
s += a
702+
return s
703+
704+
f_torch = tc.interfaces.torch_interface_kws(f)
705+
f_torch(torch.ones([2]), i=3)
706+
707+
690708
We also provider wrapper of quantum function for torch module as :py:meth:`tensorcircuit.TorchLayer` alias to :py:meth:`tensorcircuit.torchnn.QuantumNet`.
691709
692710
For ``TorchLayer``, ``use_interface=True`` is by default, which natively allow the quantum function defined on other tensorcircuit backends, such as jax or tf for speed consideration.

docs/source/sharpbits.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,53 @@ Similarly, conditional gate application must be takend carefully.
141141
# <tf.Tensor: shape=(2,), dtype=complex64, numpy=array([0.99999994+0.j, 0. +0.j], dtype=complex64)>
142142
143143
144+
Tensor variables consistency
145+
-------------------------------------------------------
146+
147+
148+
All tensor variables' backend (tf vs jax vs ..), dtype (float vs complex), shape and device (cpu vs gpu) must be compatible/consistent.
149+
150+
Inspect the backend, dtype, shape and device using the following codes.
151+
152+
.. code-block:: python
153+
154+
for backend in ["numpy", "tensorflow", "jax", "pytorch"]:
155+
with tc.runtime_backend(backend):
156+
a = tc.backend.ones([2, 3])
157+
print("tensor backend:", tc.interfaces.which_backend(a))
158+
print("tensor dtype:", tc.backend.dtype(a))
159+
print("tensor shape:", tc.backend.shape_tuple(a))
160+
print("tensor device:", tc.backend.device(a))
161+
162+
If the backend is inconsistent, one can convert the tensor backend via :py:meth:`tensorcircuit.interfaces.tensortrans.general_args_to_backend`.
163+
164+
.. code-block:: python
165+
166+
for backend in ["numpy", "tensorflow", "jax", "pytorch"]:
167+
with tc.runtime_backend(backend):
168+
a = tc.backend.ones([2, 3])
169+
print("tensor backend:", tc.interfaces.which_backend(a))
170+
b = tc.interfaces.general_args_to_backend(a, target_backend="jax", enable_dlpack=False)
171+
print("tensor backend:", tc.interfaces.which_backend(b))
172+
173+
If the dtype is inconsistent, one can convert the tensor dtype using ``tc.backend.cast``.
174+
175+
.. code-block:: python
176+
177+
for backend in ["numpy", "tensorflow", "jax", "pytorch"]:
178+
with tc.runtime_backend(backend):
179+
a = tc.backend.ones([2, 3])
180+
print("tensor dtype:", tc.backend.dtype(a))
181+
b = tc.backend.cast(a, dtype="float64")
182+
print("tensor dtype:", tc.backend.dtype(b))
183+
184+
Also note the jax issue on float64/complex128, see `jax gotcha <https://github.com/google/jax#current-gotchas>`_.
185+
186+
If the shape is not consistent, one can convert the shape by ``tc.backend.reshape``.
187+
188+
If the device is not consistent, one can move the tensor between devices by ``tc.backend.device_move``.
189+
190+
144191
AD Consistency
145192
---------------------
146193

docs/source/tutorial.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ Jupyter Tutorials
1919
tutorials/optimization_and_expressibility.ipynb
2020
tutorials/vqex_mbl.ipynb
2121
tutorials/dqas.ipynb
22-
tutorials/barren_plateaus.ipynb
22+
tutorials/barren_plateaus.ipynb
23+
tutorials/qaoa_portfolio_optimization.ipynb

docs/source/tutorials/qaoa.ipynb

Lines changed: 67 additions & 56 deletions
Large diffs are not rendered by default.

docs/source/tutorials/qaoa_portfolio_optimization.ipynb

Lines changed: 602 additions & 0 deletions
Large diffs are not rendered by default.

tensorcircuit/cloud/apis.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
try:
2929
from . import quafu_provider
3030
except (ImportError, ModuleNotFoundError):
31-
logger.warning("fail to load cloud provider module: quafu")
31+
pass
32+
# logger.warning("fail to load cloud provider module: quafu")
3233

3334
package_name = "tensorcircuit"
3435
thismodule = sys.modules[__name__]

tensorcircuit/interfaces/torch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def f(tensor, integer):
135135
r += tensor
136136
return r
137137
138-
fnew = torch_interface_kws(f)
138+
fnew = tc.interfaces.torch_interface_kws(f)
139139
140140
print(fnew(torch.ones([2]), integer=3))
141141
print(fnew(torch.ones([2]), integer=4))

0 commit comments

Comments
 (0)