Skip to content

Commit

Permalink
Standard Q# Sample Entry Points and Implicit Namespaces (#2023)
Browse files Browse the repository at this point in the history
- Changes the samples with non-standard entry point names to use
`Main()` as their entry point, removing the need for the `@EntryPoint()`
attribute.
- Removes explicit namespaces in the samples.
  • Loading branch information
ScottCarda-MS authored Nov 15, 2024
1 parent 2edde75 commit 91f754e
Show file tree
Hide file tree
Showing 62 changed files with 8,698 additions and 10,164 deletions.
2 changes: 1 addition & 1 deletion npm/qsharp/test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ test("Run samples", async () => {

for await (const sample of testCases) {
await compiler.run(
{ sources: [[sample.title, sample.code]] },
{ sources: [["main.qs", sample.code]] },
"",
1,
resultsHandler,
Expand Down
102 changes: 49 additions & 53 deletions samples/algorithms/BellState.qs
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,60 @@
/// that represent the simplest (and maximal) examples of quantum entanglement.
///
/// This Q# program implements the four different Bell states.
namespace Sample {
import Std.Diagnostics.*;
import Std.Measurement.*;
import Std.Diagnostics.*;

@EntryPoint()
operation BellStates() : (Result, Result)[] {
// This array contains a label and a preparation operation for each one
// of the four Bell states.
let bellStateTuples = [
("|Φ+〉", PreparePhiPlus),
("|Φ-〉", PreparePhiMinus),
("|Ψ+〉", PreparePsiPlus),
("|Ψ-〉", PreparePsiMinus)
];
operation Main() : (Result, Result)[] {
// This array contains a label and a preparation operation for each one
// of the four Bell states.
let bellStateTuples = [
("|Φ+〉", PreparePhiPlus),
("|Φ-〉", PreparePhiMinus),
("|Ψ+〉", PreparePsiPlus),
("|Ψ-〉", PreparePsiMinus)
];

// Prepare all Bell states, show them using the `DumpMachine` operation
// and measure the Bell state qubits.
mutable measurements = [];
for (label, prepare) in bellStateTuples {
// Allocate the two qubits that will be used to create a Bell state.
use register = Qubit[2];
prepare(register);
Message($"Bell state {label}:");
DumpMachine();
set measurements += [(MResetZ(register[0]), MResetZ(register[1]))];
}
return measurements;
// Prepare all Bell states, show them using the `DumpMachine` operation
// and measure the Bell state qubits.
mutable measurements = [];
for (label, prepare) in bellStateTuples {
// Allocate the two qubits that will be used to create a Bell state.
use register = Qubit[2];
prepare(register);
Message($"Bell state {label}:");
DumpMachine();
set measurements += [(MResetZ(register[0]), MResetZ(register[1]))];
}
return measurements;
}

/// # Summary
/// Prepares |Φ+⟩ = (|00⟩+|11⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePhiPlus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 + |11〉)
}
/// # Summary
/// Prepares |Φ+⟩ = (|00⟩+|11⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePhiPlus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 + |11〉)
}

/// # Summary
/// Prepares |Φ−⟩ = (|00⟩-|11⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePhiMinus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
Z(register[0]); // |-0〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 - |11〉)
}
/// # Summary
/// Prepares |Φ−⟩ = (|00⟩-|11⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePhiMinus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
Z(register[0]); // |-0〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|00〉 - |11〉)
}

/// # Summary
/// Prepares |Ψ+⟩ = (|01⟩+|10⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePsiPlus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
X(register[1]); // |+1〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 + |10〉)
}
/// # Summary
/// Prepares |Ψ+⟩ = (|01⟩+|10⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePsiPlus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
X(register[1]); // |+1〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 + |10〉)
}

/// # Summary
/// Prepares |Ψ−⟩ = (|01⟩-|10⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePsiMinus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
Z(register[0]); // |-0〉
X(register[1]); // |-1〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 - |10〉)
}
/// # Summary
/// Prepares |Ψ−⟩ = (|01⟩-|10⟩)/√2 state assuming `register` is in |00⟩ state.
operation PreparePsiMinus(register : Qubit[]) : Unit {
H(register[0]); // |+0〉
Z(register[0]); // |-0〉
X(register[1]); // |-1〉
CNOT(register[0], register[1]); // 1/sqrt(2)(|01〉 - |10〉)
}
Loading

0 comments on commit 91f754e

Please sign in to comment.