Skip to content

Commit

Permalink
GHZ and CAT samples work in Adaptive and Base Profiles (#1532)
Browse files Browse the repository at this point in the history
This updates GHZ state and CAT state sample to work in base profile and
adaptive profile.
- Removed check for zero state
- updated comments
- Used library functions MResetEachZ() and Rest()
- Made preparation function specific to GHZ in GHZ sample

---------

Co-authored-by: Dmitry Vasilevsky <[email protected]>
  • Loading branch information
DmitryVasilevsky and Dmitry Vasilevsky authored May 20, 2024
1 parent 5a60039 commit 481d436
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
14 changes: 6 additions & 8 deletions samples/algorithms/CatState.qs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/// This Q# program implements a cat state of 5 qubits.
namespace Sample {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Measurement;

@EntryPoint()
operation Main() : Result[] {
Expand All @@ -21,18 +20,17 @@ namespace Sample {
DumpMachine();

// Measure and reset qubits before releasing them.
let results = MeasureEachZ(register);
ResetAll(register);
return results;
MResetEachZ(register)
}

// Prepares a cat state 1/sqrt(2)(|0...0〉 + |1...1〉) using a qubit register
// in the zero state.
/// # Summary
/// Prepares state (|0...0〉 + |1...1〉) / √2 (a generalized GHZ state
/// or a cat state) across the `register` of qubits.
/// All qubits are assumed to be in |0〉 state on input.
operation PrepareCatState(register : Qubit[]) : Unit {
Fact(Length(register) > 0, "Qubit register must not be empty.");
Fact(CheckAllZero(register), "Qubits are not in the |0〉 state.");

// Set the first qubit in the register into a 1/sqrt(2)(|0〉 + |1〉) superposition.
// Set the first qubit in the register into a (|0〉 + |1〉) / √2 superposition.
// Then apply a CNOT to the remaining qubits using the first qubit as control.
H(register[0]);
ApplyToEach(CNOT(register[0], _), register[1...]);
Expand Down
20 changes: 7 additions & 13 deletions samples/algorithms/GHZ.qs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
/// returns the result of measuring those qubits.
namespace Sample {
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Measurement;

@EntryPoint()
operation Main() : Result[] {
Expand All @@ -30,23 +29,18 @@ namespace Sample {
DumpMachine();

// Measure and reset qubits before releasing them.
let results = MeasureEachZ(qs);
ResetAll(qs);
return results;
MResetEachZ(qs)
}

/// # Summary
/// This operation prepares a generalized GHZ state across a register of qubits.
///
/// # Input
/// ## qs
/// The given register of qubits to be transformed into the GHZ state. It is assumed
/// that these qubits are in their default |0〉 state.
/// Prepares state (|000〉 + |111〉) / √2 (GHZ state) across a register
/// of three qubits `qs`.
/// All qubits are assumed to be in |0〉 state on input.
operation PrepareGHZState(qs : Qubit[]) : Unit {
Fact(Length(qs) > 0, "`qs` length must be greater than zero");
Fact(CheckAllZero(qs), "All qubits must be in the |0〉 state");
Fact(Length(qs) == 3, "`qs` length shuold be 3.");

H(qs[0]);
ApplyToEach(CNOT(qs[0], _), qs[1...]);
CNOT(qs[0], qs[1]);
CNOT(qs[0], qs[2]);
}
}

0 comments on commit 481d436

Please sign in to comment.