Skip to content

Conversation

@WeiguoMa
Copy link
Contributor

@WeiguoMa WeiguoMa commented Sep 3, 2025

This PR makes following changes:

  1. quditcircuit.py, this file contains tc.QuditCircuit for establishing a quadit circuit.
  2. quditgates.py, this file provides basic gates in qudit systems.
  3. tests/test_quditcircuit.py and tests/test_quditgates.py.
  4. add interface to tc.init file.

Copy link
Member

@refraction-ray refraction-ray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general implementation now looks good to me, some minor changes required to minimize and clean up the qudit vqe example

Copy link
Member

@refraction-ray refraction-ray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more fixes required to make the example script more clean, imagine using jax backend on GPU, you will not like the conversion to numpy happen during the computation as it requires CPU-GPU data transfer which is slow, so try to avoid such conversion unless it only happens at the very beginning stage

"""
bk = tc.backend
# Keep differentiability by passing backend scalars into gates
plist = [params_b[i] for i in range(params_b.shape[0])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not directly use params_b?

) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, float]:
N = np.diag(np.arange(d))
Xsym = symmetrize_hermitian(tc.backend.numpy(tc.quditgates._x_matrix_func(d)))
Zsym = symmetrize_hermitian(tc.backend.numpy(tc.quditgates._z_matrix_func(d)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to transform to numpy?

return bk.real(E)


def energy_expectation_numpy(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function can be exactly the same with the backend version?

bk = tc.backend
if jit:
energy_bk = bk.jit(energy_bk)
grad_f = bk.grad(energy_bk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interchange the order of jit and grad, should be jit(grad(f))

x_b = bk.convert_to_tensor(x_np) # numpy -> backend tensor
g_b = grad_f(x_b) # backend gradient
g = to_np(g_b) # backend -> numpy
x_np = x_np - lr * g # SGD step in numpy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why sgd in numpy? just keep to the corresponding backend and avoid those conversions from and to numpy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the logic is complicated with random search, you can remove the random search and numpy backend

@WeiguoMa
Copy link
Contributor Author

WeiguoMa commented Sep 9, 2025

Hi, Shixin. I tried to make a ``new'' file here. This should be totally different and cleaner without any complicated conversion.

@refraction-ray
Copy link
Member

nice work, LGTM!

@refraction-ray refraction-ray merged commit 0a93cbe into tensorcircuit:master Sep 9, 2025
1 check passed
@refraction-ray
Copy link
Member

refraction-ray commented Sep 9, 2025

Some remaining work for a new PR:

  1. with the output state from qudit circuit, add some tests on these quantum information/entanglement quantities for d>2.
  2. an example script for MPSCircuit using dim>2 and c.unitary for gates application to check that MPSCircuit is working for qudit case
  3. docstring adjustment if any based on the docstring rendering results on readthedocs for QuditCircuit APIs

@refraction-ray
Copy link
Member

@all-contributors please add @WeiguoMa for examples

@allcontributors
Copy link
Contributor

@refraction-ray

I've put up a pull request to add @WeiguoMa! 🎉

@WeiguoMa WeiguoMa deleted the qudit_circuit_gate branch September 9, 2025 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants