Cirq Fundamentals

Lesson 1 of 8

Setup and Your First Circuit

Cirq is Google's open-source Python SDK for quantum computing. It covers the same core ideas as any quantum SDK, qubits, gates, superposition, entanglement, and measurement, with its own particular style: circuits are built by appending operations (a gate applied to specific qubits) rather than calling a method per gate type.

Installing Cirq

Like Qiskit, Cirq depends on compiled numerical libraries not available in this course's browser sandbox, write and run this project locally with a real Python install.

pip install cirq

Qubits and circuits

import cirq

qubit = cirq.LineQubit(0)
circuit = cirq.Circuit()
circuit.append(cirq.X(qubit))
circuit.append(cirq.measure(qubit, key='result'))

print(circuit)
  • cirq.LineQubit(0) creates a qubit identified by position 0 on an imaginary line, Cirq's qubits are explicit objects you create and pass around, rather than implicit indices into a circuit.
  • cirq.Circuit() starts an empty circuit, and .append(...) adds operations to it, one at a time (or as a list).
  • cirq.X(qubit) is an operation: the X gate applied to a specific qubit. In Cirq, gates (cirq.X) and operations (a gate applied to a qubit, cirq.X(qubit)) are distinct: a gate is reusable and qubit-agnostic, an operation is that gate bound to a specific target.
  • cirq.measure(qubit, key='result') measures the qubit and tags the result with a string key, used later to look up that specific measurement's outcome (Cirq doesn't use a separate classical register the way Qiskit does).
  • print(circuit) renders an ASCII diagram of the circuit, just like Qiskit's draw().

NOTE

Every code block in this course needs a real local Python + Cirq install to run, quantum simulation depends on compiled numerical libraries this course's browser sandbox doesn't have. Treat this course like the Pygame or Kivy projects: read, understand, and run the code on your own machine.

📝 Setup and Your First Circuit Quiz

Passing score: 70%
  1. 1.In Cirq, what is the difference between a gate and an operation?

  2. 2.cirq.measure(qubit, key='result') tags a measurement with a string key used to look up its outcome later.

  3. 3.circuit.____(operation) adds an operation to a Cirq circuit.