Cirq Fundamentals

Lesson 5 of 8

Measurement and Running the Simulator

Like Qiskit's shot-based AerSimulator, Cirq's .run() method executes a circuit with measurement many times and tallies the results, this is what a real device would give you, no direct access to amplitudes.

Running with repetitions

import cirq

q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit([
    cirq.H(q0),
    cirq.CNOT(q0, q1),
    cirq.measure(q0, q1, key='result'),
])

simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=1024)
counts = result.histogram(key='result')
print(counts)  # Counter({0: ~512, 3: ~512})
  • repetitions=1024 is Cirq's name for what Qiskit calls shots, run the whole circuit 1024 independent times.
  • result.histogram(key='result') tallies outcomes for the measurement tagged 'result', returned as a Counter. Notice the keys are integers, not bitstrings like Qiskit's '00'/'11', Cirq encodes a multi-qubit measurement as one combined integer (0 = 00, 3 = 11 for 2 qubits), where Qiskit keeps it as a string.

Converting to a bitstring, if you want one

for value, count in counts.items():
    bitstring = format(value, '02b')  # pad to 2 bits
    print(bitstring, '->', count)

format(value, '02b') converts an integer to its binary string representation, padded to 2 digits, exactly the inverse of the int(bitstring, 2) conversion from the RNG lesson.

Statistical noise, same as any SDK

result_small = simulator.run(circuit, repetitions=10)
print(result_small.histogram(key='result'))  # could easily be lopsided with so few repetitions

Exactly like Qiskit, fewer repetitions means a noisier estimate of the true 50/50 distribution, this isn't an SDK quirk, it's the statistics of sampling any random process a small number of times.

NOTE

counts only ever contains 0 (00) and 3 (11), the same entanglement result from the previous lesson's exact Statevector-equivalent probabilities, now observed empirically through repeated measurement.

📝 Measurement and Simulation Quiz

Passing score: 70%
  1. 1.What is Cirq's name for what Qiskit calls 'shots'?

  2. 2.result.histogram(key=...) returns outcomes keyed by integer, not bitstring, for a multi-qubit measurement.

  3. 3.format(value, '02b') converts an integer to a binary string, padded to ____ digits.