# Measurement in OpenQASM

Quantum measurement is a critical operation in quantum computing that translates the qubit states into classical bits we can interpret. This chapter will guide you through the measurement syntax in OpenQASM and show you how to capture the states of qubits in classical registers.

## Understanding Quantum Measurement

Quantum measurements in OpenQASM default to the computational (Z) basis, collapsing the qubit state to either \( |0\rangle \) or \( |1\rangle \) upon observation. This process is analogous to reading out a classical bit with a definite value of 0 or 1. OpenQASM simplifies measuring individual qubits or an entire quantum register with ease.

## Measure a Single Qubit

Measuring a single qubit's state involves the `measure`

command. Here's the syntax for measuring a particular qubit from a quantum register and storing its state in a classical register:

```
measure <quantum_register>[<qubit_index>] -> <classical_register>[<bit_index>];
```

### Experiment: Measuring the First Qubit

To illustrate this, we'll measure a single qubit from our quantum register^{.css-6phhhi{transition-property:var(--chakra-transition-property-common);transition-duration:var(--chakra-transition-duration-fast);transition-timing-function:var(--chakra-transition-easing-ease-out);cursor:pointer;outline:2px solid transparent;outline-offset:2px;position:relative;color:var(--chakra-colors-brand-600);-webkit-text-decoration:none;text-decoration:none;width:-webkit-fit-content;width:-moz-fit-content;width:fit-content;}.css-6phhhi:focus-visible,.css-6phhhi[data-focus-visible]{box-shadow:var(--chakra-shadows-outline);}.css-6phhhi:hover,.css-6phhhi[data-hover]{color:var(--chakra-colors-brand-600);}.css-6phhhi:hover::before,.css-6phhhi[data-hover]::before{-webkit-transform:scaleX(1);-moz-transform:scaleX(1);-ms-transform:scaleX(1);transform:scaleX(1);}.css-6phhhi::before{content:"";position:absolute;display:block;width:100%;height:2px;bottom:calc(var(--chakra-space-0-5) * -1);left:0px;background-color:var(--chakra-colors-brand-600);-webkit-transform:scaleX(0);-moz-transform:scaleX(0);-ms-transform:scaleX(0);transform:scaleX(0);transform-origin:top left;-webkit-transition:-webkit-transform 0.3s ease;transition:transform 0.3s ease;}1}^{2}^{3}^{4}:

Diagram

`OPENQASM 2.0; // Define the OpenQASM version [^1] include "qelib1.inc"; // Include the standard library [^2] // Declaration of quantum and classical registers qreg qubit[1]; // Single-qubit quantum register [^3] creg bit[1]; // Single-bit classical register [^3] // Measure the qubit and save the result in the classical bit measure qubit[0] -> bit[0]; // Measurement command [^4]`

Translation

Powered by Perceval, Qiskit, PyZX

Not run yet

Simulation

Not run yet

This experiment produces a classical bit that corresponds to the measured state of our quantum register's qubit.

## Measure a Quantum Register

For simultaneous measurement of all qubits in a quantum register, the same `measure`

keyword is used:

```
measure <quantum_register> -> <classical_register>;
```

### Experiment: Measuring Multiple Qubits

Let's extend our measurement to an entire register^{1}^{2}^{3}^{4}:

Diagram

`OPENQASM 2.0; // Define the OpenQASM version [^1] include "qelib1.inc"; // Include the standard library [^1] // Declare quantum and classical registers with matching sizes qreg qubits[2]; // Quantum register with two qubits [^3] creg bits[2]; // Classical register with two bits [^3] // Execute measurements on all qubits and store the results measure qubits -> bits; // Measurement command for all qubits [^4]`

Translation

Powered by Perceval, Qiskit, PyZX

Not run yet

Simulation

Not run yet

In this scenario, each qubit from the `qubits`

register has its measured state stored in the corresponding bit of the `bits`

register. For instance, `qubits[0]`

's state would be stored in `bits[0]`

, and so on.

**Note:** The `measure`

command in OpenQASM is versatile, accommodating measurements of single qubits or entire registers depending on your program's needs.

The ability to measure qubits and store their outcomes as classical information is a fundamental feature of OpenQASM, bridging the gap between quantum operations and traditional computing.