1
$\begingroup$

I am trying to use the DensityMatrixSimulator in Cirq. I want to append gates to the circuit conditional on measurements. In order to reduce computational resources, I am trying to run these conditional gates by initializing another circuit with these conditional gates using the final state of the first measurement as the initial state. However, I am not sure how I can get the simulate method to accept the initial state matrix (see code below).

I get the ValueError of

ValueError: `state_like` was convertible to a numpy array, but its shape was neither the shape of a list of computational basis values (`len(qid_shape)`) nor the shape of a list or tensor of state vector amplitudes (`qid_shape` or `(product(qid_shape),)`.

qid_shape=(2, 2)
np.array(state_like).shape=(2, 2, 2, 2)
np.array(state_like)=[[[[ 0.+0.j  0.-0.j]
   [ 0.+0.j  0.-0.j]]

  [[-0.+0.j  0.-0.j]
   [-0.+0.j  0.-0.j]]]


 [[[ 0.+0.j  0.-0.j]
   [ 1.+0.j  0.-0.j]]

  [[-0.+0.j  0.-0.j]
   [-0.+0.j  0.-0.j]]]]

This is the code I am trying to run to figure out how to initialize the initial state of the second circuit.


qubits = cirq.LineQubit.range(2)

circuit = cirq.Circuit()

circuit.append(cirq.X(qubits[0]))
circuit.append(cirq.Z(qubits[1]))

s = cirq.DensityMatrixSimulator()

results = s.simulate(circuit)

r = cirq.DensityMatrixSimulator()

circuit2 = cirq.Circuit()

circuit2.append(cirq.X(qubits[0]))

circuit2.append(cirq.Z(qubits[1]))

results2 =r.simulate(circuit2, initial_state = results._final_simulator_state.density_matrix)

Thank you in advance!

$\endgroup$
2
$\begingroup$

Instead of accessing results._final_simulator_state.density_matrix, which has a leading underscore implying you shouldn't be using it or relying on it to stay stable, use results.final_density_matrix. Making that substitution seems to result in the code working.

Separately, I think the fact that what you did doesn't work is a bug. It seems that the method doesn't recognize the shape (2,) * 2 * num_qubits as a valid shape for the density matrix as a tensor. I opened https://github.com/quantumlib/Cirq/issues/3958 to get it fixed.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.