Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

The default dtype of np.complex64 for the DensityMatrixSimulator is not precise enough for relatively small circuits.

Open bramathon opened this issue 10 months ago • 1 comments

Description of the issue

How to reproduce the issue

Use the example circuit: example-circuit.json

import cirq

with open("example-circuit.json", "r") as f:
    circuit = cirq.read_json(f)

simulator = cirq.DensityMatrixSimulator(dtype=np.complex64, noise=None, seed=None, split_untangled_states=True)
obs = cirq.PauliString(cirq.X(cirq.LineQubit(28)), cirq.X(cirq.LineQubit(29)))
simulator.simulate_expectation_values(program=circuit, observables=obs)

Now, if we change the dtype to np.complex128, the error goes away.

Suggested solution: set the default value of dtype to np.complex128 or np.complex256

1018 raise ValueError(f'Density matrix does not have trace 1. Instead, it has trace {trace}.') 1019 if not np.all(np.linalg.eigvalsh(density_matrix) > -atol): -> 1020 raise ValueError('The density matrix is not positive semidefinite.')

ValueError: The density matrix is not positive semidefinite.

Cirq version 1.3.0

bramathon avatar Apr 05 '24 14:04 bramathon

csynkque meeting - this works as intended, but the error message can be improved to suggest using a higher precision complex128 type. Another consideration is to expose a tolerance atol argument which would allow higher round-offs for complex64 data type.

pavoljuhas avatar Apr 10 '24 17:04 pavoljuhas