Skip to content

[Feature Suggestion]Density matrix measurement #227

@yezhuoyang

Description

@yezhuoyang

During the development of density matrix module, I tried to run the same mnist example on a noise device. I made the following commit:

[Example] Add the minist example that run on noisedevice

However, in the current interface design, the quantum state measurement is not compatible with the measurement on the density matrix, more specifically, I got the following error message:

[2024-02-02 22:54:14.521] Only use the front 75 images as TEST set.
Epoch 1:
Traceback (most recent call last):
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\examples\mnist\mnist_noise.py", line 250, in <module>
    main()
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\examples\mnist\mnist_noise.py", line 236, in main
    train(dataflow, model, device, optimizer)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\examples\mnist\mnist_noise.py", line 142, in train
    outputs = model(inputs)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\venv\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\venv\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\examples\mnist\mnist_noise.py", line 129, in forward
    x = self.measure(qdev)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\venv\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\venv\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\torchquantum\measurement\measurements.py", line 324, in forward
    x = expval(qdev, list(range(qdev.n_wires)), [self.obs()] * qdev.n_wires)
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\torchquantum\measurement\measurements.py", line 287, in expval
    all_dims = np.arange(qdev.states.dim())
  File "C:\Users\yezhu\OneDrive\Desktop\torchquantum\venv\lib\site-packages\torch\nn\modules\module.py", line 1695, in __getattr__
    raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'NoiseDevice' object has no attribute 'states'

The problem is that in the code in the measurement module, all functions assume that we have a state vector device, so it will try to call the "states" member of the device.

measurement/measurements.py

In the new interface design, either we should change the code in the measurement.py, so that it is compatible with the density matrix measurement, or we can directly define some new measurement functions, but also change the code in the forward and backpropagation.

Metadata

Metadata

Labels

bugSomething isn't workingenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions