microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9c33427155ee7a0d2f2beebc2f03d7c4609b6e4f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/getting_started/Measurement.qs

34lines · modepreview

/// # Summary
/// Measurement sample
///
/// # Description
/// This Q# program demonstrates how to perform measurements in Z basis.
///
/// # Remarks
/// Quantum measurement is an irreversible operation in which a quantum system
/// is manipulated to yield a numerical result. Measuring a quantum system
/// generally changes the quantum state that describes that system.
operation Main() : (Result, Result[]) {
    // Allocate a qubit. Qubit is in |0〉 state after allocation.
    use q = Qubit();
    // Flip the state. Qubit is in |1〉 state now.
    X(q);

    // The `MResetZ` operation performs a measurement of a single qubit in the
    // computational basis, also known as the Pauli Z basis. Then it resets
    // the qubit to |0〉 state. `MResetZ` may be more efficient than measuring
    // a qubit and resetting it using two separate operations.
    let result = MResetZ(q);

    // Allocate a two-qubit array (or register).
    use qs = Qubit[2];

    // The `MResetEachZ` operation measures each qubit in an array in the
    // computational basis and resets each qubit to |0〉 state. It returns
    // an array of `Result` values.
    let results = MResetEachZ(qs);

    // Return all results. In Q#, the result of a measurement is a value
    // of the type `Result`, that is, `One` or `Zero`.
    (result, results)
}