I could manage to produce 3 qubit ghz state in Cirq. But I don't know how I can produce $|GHZ\rangle\langle GHZ|$ in Cirq Here is my code for 3 qubit ghz state Can you help me please for improving my code from $|GHZ\rangle$ to $|GHZ\rangle\langle GHZ|$:

n = 3
qubits = cirq.LineQubit.range ( n )

def circuit ():
  circuit = cirq.Circuit ()
  circuit.append (cirq.H(qubits[0]))
  for i in range (n-1):
    circuit.append ( cirq.CNOT ( qubits [i] , qubits [i+1]) )
  circuit.append (cirq.measure (* qubits , key ='x') )
  print (circuit)
  return circuit

def simulation (circuit):
  simulator = cirq.Simulator()
  results = simulator.run ( circuit , repetitions = 200)
  counts = cirq.plot_state_histogram (results)

def main():
  simulation (circuit())
if __name__ == "__main__ ":
   main ()
  • 1
    $\begingroup$ A density matrix for a state $|\psi\rangle$ is $\rho = |\psi\rangle\langle\psi|$. In your case, $|GHZ\rangle\langle GHZ|$ is $|GHZ\rangle$ run on cirq.DensityMatrixSimulator $\endgroup$ Oct 18, 2020 at 20:18
  • $\begingroup$ Thank you very much for your answer. One more thing is that: I actually want that: p.|GHZ><GHZ|+(1-p)*rho The rho itself could also be expressed in terms of some other things though. So it will look like rho = q . rho_{1,2} + r . rho_{2,3} + s . rho{1,3} + (1-q-r-s) . rho_separable and each of these rho_{i,j} states will be bipartite entangled. How can I do that? $\endgroup$
    – quest
    Oct 18, 2020 at 20:34
  • $\begingroup$ If you can define the unitary matrix for each of these terms, then you can create a class that will represent your noisy evolution, using the mixture protocol in Cirq. cirq.readthedocs.io/en/latest/docs/… Let me know if this helps! $\endgroup$ Oct 19, 2020 at 3:56
  • $\begingroup$ It's not clear what you're asking for. Do you want a numpy array storing the matrix |GHZ><GHZ|? That's cirq.final_density_matrix(your_circuit) (well.. if you omit the measurement it is). Or do you want a representation of this state to feed into other parts of cirq? Which parts? $\endgroup$ Oct 19, 2020 at 17:01
  • $\begingroup$ Dear Balint Pato thanks you very much for your answer! Can you at least show me how I can write rho ( rho = q . rho_{1,2} + r . rho_{2,3} + s . rho{1,3} + (1-q-r-s) . rho_separable) part with Cirq? Maybe after completing this part I can just use density matrix @BalintPato $\endgroup$
    – quest
    Oct 19, 2020 at 18:40

1 Answer 1


Since you can produce the GHZ state, I will skip the corresponding code and just use that QuantumCircuit object to build a density matrix $|GHZ><GHZ|$.

from qiskit.quantum_info import DensityMatrix
# here circuit denotes the circuit that contains your GHZ state.

Or there is another way to do so(not recommended, just for reference).

from qiskit.aqua.operators import StateFn
psi=StateFn(circuit)# produce the state vector
DM=(psi@~psi).eval()# state ket tensor state bra =density matrix
# When calling StateFn function, the quantum circuit must not contain classical register

But note that the density matrix produced by the latter method is not admitted by qiskit, because the DensityMatrix object in qiskit has two attributes(the matrix itself and its dimension).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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