microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.2.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/CatState.qs

40lines · modeblame

ffd3f875César Zaragoza Cortés2 years ago1/// # Sample
2/// Cat State
3///
4/// # Description
5/// A cat state is a highly entangled state where the qubits are in a
6/// superposition of all |0...0〉 or all |1...1〉.
7///
8/// This Q# program implements a cat state of 5 qubits.
9namespace Sample {
10open Microsoft.Quantum.Diagnostics;
11open Microsoft.Quantum.Measurement;
12
13@EntryPoint()
14operation Main() : Result[] {
15use register = Qubit[5];
16
17// Prepare a cat state using the allocated register.
18PrepareCatState(register);
19
20// Show the cat state.
21DumpMachine();
22
23// Measure and reset qubits before releasing them.
24let results = MeasureEachZ(register);
25ResetAll(register);
26return results;
27}
28
29// Prepares a cat state 1/sqrt(2)(|0...0〉 + |1...1〉) using a qubit register
30// in the zero state.
31operation PrepareCatState(register : Qubit[]) : Unit {
32Fact(Length(register) > 0, "Qubit register must not be empty.");
33Fact(CheckAllZero(register), "Qubits are not in the |0〉 state.");
34
35// Set the first qubit in the register into a 1/sqrt(2)(|0〉 + |1〉) superposition.
36// Then apply a CNOT to the remaining qubits using the first qubit as control.
37H(register[0]);
38ApplyToEach(CNOT(register[0], _), register[1...]);
39}
40}