Skip to content
Current section
The Quantum Computing Enthusiast’s Hub
Saved
All sections

All sections

Saved
Skip Learn navigation
Encyclopedia
Topic

Cirq reference semantics

A quantum-circuit framework with explicit qubit objects, operations, moments and simulation interfaces.

Also called Cirq

Edition released Coverage: Overview · Technical
On this page

Definition and significance

Cirq represents operations on named or structured qubit objects. A moment groups operations that can occur together in the program description. This grouping is not by itself a calibrated hardware schedule. Its simulation interfaces distinguish sampled measurements from access to a computed final statevector.

Mechanism and mathematical treatment

An original Bell-state description is cirq.Circuit(cirq.H(a),cirq.CNOT(a,b)). For a statevector comparison, specify qubit_order=[a,b]. That order determines tensor-factor interpretation. A call returning a final statevector exposes a simulator's internal representation; a hardware measurement returns sampled outcomes.

Fully worked reference example

With input |00> and order [a,b], H(a) gives [1/sqrt(2),0,1/sqrt(2),0]; CNOT(a,b) gives [1/sqrt(2),0,0,1/sqrt(2)]. To verify ordering, use the asymmetric X(a) example instead: its vector is [0,0,1,0]. Reversing the declared order to [b,a] changes that vector to [0,1,0,0] while the labeled state is unchanged.

Limitations and common misconceptions

Do not infer physical parallelism from a moment alone. Device constraints and durations can require further scheduling. Version-pin a published snippet and mark whether it was executed; these conceptual fragments were not run against a pinned Cirq installation.

Scientific relationships

The declared qubit order fixes tensor factors in numerical output.

A moment-structured circuit with an asymmetric prediction

Use labeled qubits a and b. The first conceptual moment contains H on a and R_Y(theta) on b. The second contains CZ(a,b), and the third contains R_Y(beta) on a. The first two gates act on disjoint qubits, so their order within that moment does not change the ideal unitary. The moment structure describes the circuit's operation grouping; any physical duration model remains separate.

Before the final rotation, the state is [cos(theta/2)(|00>+|10>)+sin(theta/2)(|01>-|11>)]/sqrt(2), in the explicitly chosen order [a,b]. Its local expectations are <X_a>=cos(theta), <Z_a>=0 and <Z_b>=cos(theta). Also <X_a Z_b>=1. Conjugating the final R_Y(beta) gives <Z_a>=-cos(theta)sin(beta) and <Z_a Z_b>=-sin(beta).

At theta=pi/3 and beta=pi/6, the four computational probabilities in [a,b] order are 3/16,3/16,9/16,1/16 for 00,01,10,11. They follow from P(a,b)=[1+(-1)^a<Z_a>+(-1)^b<Z_b>+(-1)^(a+b)<Z_aZ_b>]/4, using local means -1/4 and 1/2 and correlation -1/2. This complete distribution is an original analytic reference for a conceptual circuit, not an executed Cirq result.

Tensor order and measurement-key order are separate

An explicit simulator qubit order [a,b] makes a the outer tensor factor in the reference state. Reversing it to [b,a] exchanges the probabilities of labels 01 and 10 while preserving the physical labeled state. Named qubit objects identify the systems; the supplied order identifies how a numerical array represents them. Default ordering should not be inferred from the order in which variable names appear in a paragraph.

A measurement operation that groups a and b under one key also has an operand order for its reported bit columns. Two separate measurement keys instead identify separate results. The scientific decoding rule should state which column or key corresponds to a and which to b before calculating parity or a local mean. A dictionary's display order is not a reliable substitute for that contract.

For the distribution above, a has probability 5/8 of being one, whereas b has probability 1/4 of being one. Swapping the two identities would therefore change their marginal statistics. Their ZZ correlation is symmetric and would remain -1/2, so checking only parity would miss the labeling error. This is why an asymmetric local observable belongs in a cross-interface reference test.

Parameter resolution and a small sweep

Keep beta=pi/6 and choose theta equal to zero, pi/2 and pi. The predicted Z_a expectations are -1/2,0,+1/2, respectively. The ZZ expectation remains -1/2 throughout because it depends only on beta in this circuit. A parameter sweep can therefore test both a changing local response and an invariant correlation, with no need to compare entire statevectors.

The symbolic parameter names must be resolved to numerical values before a concrete simulation or execution. A sweep over named bindings is a set of circuit instances, not one coherent superposition over parameter values. Results from different bindings should retain their association with the corresponding parameter map. Reordering a batch without reordering its labels can produce a plausible but scientifically incorrect response curve.

If each local Z expectation uses N independent ideal shots, its sample-mean variance is [1-<Z_a>^2]/N. The three sweep points therefore have variances 3/(4N),1/N and 3/(4N). Equal shot counts do not give equal uncertainty. These predicted variances can accompany a hypothetical sampling plan without inventing any observed data.

Simulation state, sampled output and execution scope

A pure-state simulation can return amplitudes for the ideal unitary circuit. A sampling interface instead returns outcomes of declared measurements. Mid-circuit measurements require conditional evolution, so a final statevector from one sampled branch is not automatically the ensemble state. A mixed-state representation or an explicit average over branches is needed to describe that ensemble.

The official simulation guide documents these distinctions and explicit qubit-order control. Its live API examples are version-sensitive. This extension relies only on the documented semantic categories and independently derives its own circuit outputs. It does not claim that a particular constructor, result attribute or backend default was executed in the present environment. The executed reference below instead uses a precise NumPy version, explicit tensor order, and deterministic double-precision matrices to check the circuit mathematics.

For this two-qubit circuit, direct four-by-four matrix multiplication is a complete classical alternative and verification method. A density-matrix evaluation can reproduce the same probabilities for the pure input. These checks establish the reference mathematics, while a device execution would additionally depend on gate realization, measurement and target constraints.

Executed reference: ordered moments and a parameter sweep

The first Kronecker product represents the simultaneous H on a and RY(theta) on b; these gates commute because their supports differ. The controlled-Z then precedes the final RY(beta) on a. With theta = pi/3 and beta = pi/6, the program prints [0.1875, 0.1875, 0.5625, 0.0625], followed by expectations [-0.25, 0.5, -0.5] for Za, Zb, ZaZb. Its three sweep rows for theta equal to zero, pi/2, and pi are [-0.5, -0.5], [0, -0.5], and [0.5, -0.5], listing Za and ZaZb. Roundoff may print a signed zero.

The fixed beta rotation changes the local a marginal while the indicated parity remains constant throughout this sweep. This makes the example sensitive to accidentally exchanging a and b. The function returns an explicitly ordered statevector, with a as its left tensor factor, and therefore has no implicit SDK qubit-order default. The numerical results were executed under the identified NumPy contract; Cirq's constructor syntax, scheduling behavior, and simulator implementation were not executed.

python · Python 3.12.16; NumPy 2.3.5 · Executed locally as an ideal mathematical reference; no quantum SDK or device used.

import numpy as np
np.set_printoptions(precision=8, suppress=True)
I = np.eye(2)
H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
Z = np.diag([1, -1])
CZ = np.diag([1, 1, 1, -1])
def ry(angle):
    c, s = np.cos(angle / 2), np.sin(angle / 2)
    return np.array([[c, -s], [s, c]])
def state(theta, beta):
    first = np.kron(H, ry(theta)) @ np.array([1, 0, 0, 0])
    return np.kron(ry(beta), I) @ CZ @ first
operators = [np.kron(Z, I), np.kron(I, Z), np.kron(Z, Z)]
def means(v):
    return np.array([np.vdot(v, op @ v).real for op in operators])
v = state(np.pi / 3, np.pi / 6)
print(np.abs(v) ** 2)
print(means(v))
assert np.allclose(np.abs(v) ** 2, np.array([3, 3, 9, 1]) / 16)
for theta in (0, np.pi / 2, np.pi):
    print(means(state(theta, np.pi / 6))[[0, 2]])

Sources

  • Google Quantum AI / Cirq · Documentation · quantumai.google
    Verification scope

    Recorded source check ·

    Simulation guide retrieved; explicit qubit_order semantics and distinction between state and measurement output inspected.

    Conceptual reference semantics only. No code execution or universal backend support claim.