Skip to content

Gates, bit order and coherence

After sampling, consider how a two-qubit outcome becomes a string. Qubit indices, basis-array indices and displayed string positions are related conventions, not interchangeable labels. Read the ordering metadata from the actual result.

An X gate swaps 0 and 1. Apply it only to qubit 0 of a two-qubit circuit. This asymmetric state reveals the string convention; the symmetric Bell outcomes 00 and 11 would not.

"""Use an asymmetric circuit to inspect bit order and compare Bell coherence."""

from __future__ import annotations

import json

import numpy as np

from cascaqit import Circuit


def experiment() -> dict[str, object]:
    result = (
        Circuit(2).x(0).measure_all().run(shots=16, seed=21, return_probabilities=True)
    )
    bell = np.array([1, 0, 0, 1], dtype=complex) / np.sqrt(2)
    coherent = np.outer(bell, bell.conj())
    mixture = np.diag([0.5, 0.0, 0.0, 0.5])
    x = np.array([[0, 1], [1, 0]])
    xx = np.kron(x, x)
    return {
        "asymmetric_counts": result.counts,
        "bit_order": result.metadata["bitstring_ordering"]["qubit_order"],
        "bell_z_probabilities": np.diag(coherent).real.tolist(),
        "mixture_z_probabilities": np.diag(mixture).tolist(),
        "bell_xx": float(np.trace(coherent @ xx).real),
        "mixture_xx": float(np.trace(mixture @ xx).real),
    }


if __name__ == "__main__":
    print(json.dumps(experiment(), sort_keys=True))

Download the full script

python examples/learning/foundations/gates_and_ordering.py
{
  "asymmetric_counts": {
    "10": 16
  },
  "bell_xx": 0.9999999999999998,
  "bell_z_probabilities": [
    0.4999999999999999,
    0.0,
    0.0,
    0.4999999999999999
  ],
  "bit_order": [
    "q0",
    "q1"
  ],
  "mixture_xx": 0.0,
  "mixture_z_probabilities": [
    0.5,
    0.0,
    0.0,
    0.5
  ]
}

Check asymmetric_counts against bit_order, then move the X gate to qubit 1 and predict the new string. All 16 ideal samples should agree in each case. This exercise is more informative than guessing whether the simulator uses a convention you have seen elsewhere.

Correlation alone is not the whole state

The second calculation compares ( |00⟩ + |11⟩ )/√2 with a 50/50 classical mixture of |00⟩ and |11⟩. Their Z-basis probabilities match. The Bell density matrix has off-diagonal coherence; the mixture does not.

For a density matrix ρ, the expectation of an observable O is Tr(ρO). The script computes XX: the Bell value is one and the mixture value is zero. These are exact matrix calculations, not finite-shot estimates returned by a backend.

Exercise: replace the plus sign in the Bell state with a minus sign. What changes in the Z probabilities and XX?

Answer

The Z probabilities do not change. XX becomes −1 because the relative phase changed. Neither a single Z-basis histogram nor a reversed bitstring can capture that phase difference.

Continue to Hamiltonians and observables, or build the SDK Bell circuit.

Concept reference: bit order.

中文版

SDK 1.0.8a · `8b227bff`