microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.1.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/GHZ.qs

52lines · modecode

1/// # Sample
2/// GHZ
3///
4/// # Description
5/// The Greenberger–Horne–Zeilinger state, or GHZ state, is a state with 3
6/// qubits defined as: |GHZ〉 = (|000〉 + |111〉) / √2.
7///
8/// The GHZ state is said to be a maximally entangled state, a multi-qubit
9/// state where the state of any one qubit is not separable from the state
10/// of any of the other qubits.
11///
12/// The generalized form of the GHZ state across any number of qubits is
13/// called a cat state, and the GHZ state is a special case of the cat
14/// state where the number of qubits is 3.
15///
16/// This Q# program prepares the GHZ state in a register of 3 qubits, then
17/// returns the result of measuring those qubits.
18namespace Sample {
19 open Microsoft.Quantum.Diagnostics;
20 open Microsoft.Quantum.Measurement;
21
22 @EntryPoint()
23 operation Main() : Result[] {
24 use qs = Qubit[3];
25
26 // Prepare a GHZ state using the allocated register.
27 PrepareGHZState(qs);
28
29 // Show the GHZ state.
30 DumpMachine();
31
32 // Measure and reset qubits before releasing them.
33 let results = MeasureEachZ(qs);
34 ResetAll(qs);
35 return results;
36 }
37
38 /// # Summary
39 /// This operation prepares a generalized GHZ state across a register of qubits.
40 ///
41 /// # Input
42 /// ## qs
43 /// The given register of qubits to be transformed into the GHZ state. It is assumed
44 /// that these qubits are in their default |0〉 state.
45 operation PrepareGHZState(qs : Qubit[]) : Unit {
46 Fact(Length(qs) > 0, "`qs` length must be greater than zero");
47 Fact(CheckAllZero(qs), "All qubits must be in the |0〉 state");
48
49 H(qs[0]);
50 ApplyToEach(CNOT(qs[0], _), qs[1...]);
51 }
52}