# Cirq-Measuring a State with Rotation Matrix

I have this state: $$p |\text{GHZ}\rangle \langle \text{GHZ}| + (1-p)\rho$$ And after creating this state I have this code lines:

state = p * GHZ+(1-p)* rho
state = p * GHZ + (1 - p) * rho
print(f"final state: \n {state}")
print(cirq.sample_density_matrix(state, indices=[0, 1, 2], repetitions=10))


Now I want to measure this state. I know we have cirq.measure in Cirq But I don't know which kind of measurement is used by this function (and the last line is also doing measurement if I am not wrong??)

cirq.measure(a, b, c)


I have 3 questions

1. I want to use rotation matrix and measure my state. Do we have rotation matrix in Cirq. Can you please show me how can I measure my state with rotation matrix in Cirq?

2. I want to choose x and Y randomly and I want to do measurement

3. Which kind of measurement is used by Cirq.measure() and (cirq.sample_density_matrix(state, indices=[0, 1, 2], repetitions=10))

cirq.measure(a, b, c) (cirq.sample_density_matrix(state, indices=[0, 1, 2], repetitions=10))

Best and thanks

• what is meant by GHZ Commented Oct 29, 2020 at 0:47
• This is ordinary GHZ state which is created by H gate and CNOT gate Commented Oct 29, 2020 at 0:59

1. cirq.rx ry, rz exist for rotation around X, Y and Z axes on the Bloch sphere

2. If you have to measure in non-computational bases, you will have to do the rotations yourself. In my answer to previous your question I wrote two versions: one with density matrices the other one with cirq.Circuit.

• In case of the circuit model you can use circuit.append(cirq.rx(np.pi/3)(a)) for example before the measurement, where a is the first qubit.

• in case of the density matrix you'll have to calculate the tensored unitaries with cirq.kron and cirq.unitary and then multiply the density as usual: unitary @ density_matrix @ unitary.conj().T. For example:

  # this is rx(pi/3) ⊗ I ⊗ I - the first qubit gets rotated, the other
# two remains the same
u = cirq.kron(cirq.unitary(cirq.rx(np.pi/3)), np.eye(2), np.eye(2))
# this applies the unitary evolution on the state density matrix
rotated_state = u @ state @ u.conj().T


3. cirq.measure measures in the computational basis. cirq.sample_density_matrix samples in the computational basis repeatedly see reference docs - it simulates "preparing the state and measuring it" multiple times. If you want the state after the measurement then probably cirq.measure_density_matrix is better suited.

• Thanks for the answer. I could not understand the part of density matrix. Multiply means multiplying ghz part and rho part or multiplay with tensor and unitary part. And after that how can I define angle for rotation matrix?(like the circuit part) Commented Oct 29, 2020 at 1:51
• And for the circuit part,you wrote a. Does a correspond şubat aor the state? Commented Oct 29, 2020 at 1:53
• I clarified my points please take a look Commented Oct 29, 2020 at 4:11
• Got it :) Thanks for using Cirq for your thesis! Commented Oct 29, 2020 at 23:42
• If you want measurement statistics of a U unitary in a computational basis you do "apply U and then measure in computational basis". If you want to measure in non-computational basis, but you only can measure in computational basis, that means "apply U then rotate then measure in computational basis". So, you have to cirq.meausre_density_matrix and only after the rotation (not before it). The rotation rotates the frame of reference before the measurement. After the measurement it only rotates the collapsed state which I don't think is what you want. Commented Oct 30, 2020 at 17:17