microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.6.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/algorithms/GHZ.qs

46lines · 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
21 @EntryPoint()
22 operation Main() : Result[] {
23 use qs = Qubit[3];
24
25 // Prepare a GHZ state using the allocated register.
26 PrepareGHZState(qs);
27
28 // Show the GHZ state.
29 DumpMachine();
30
31 // Measure and reset qubits before releasing them.
32 MResetEachZ(qs)
33 }
34
35 /// # Summary
36 /// Prepares state (|000〉 + |111〉) / √2 (GHZ state) across a register
37 /// of three qubits `qs`.
38 /// All qubits are assumed to be in |0〉 state on input.
39 operation PrepareGHZState(qs : Qubit[]) : Unit {
40 Fact(Length(qs) == 3, "`qs` length shuold be 3.");
41
42 H(qs[0]);
43 CNOT(qs[0], qs[1]);
44 CNOT(qs[0], qs[2]);
45 }
46}
47