From bb2955e4ccb4f38b15152e6a7f842b9c298611f5 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Thu, 6 Aug 2020 22:56:57 +0000 Subject: [PATCH 01/22] Example from private repo (written by Adam Paetznik) --- samples/error-correction/syndrome/Syndrome.cs | 111 ++++++++++++++++++ samples/error-correction/syndrome/Syndrome.qs | 53 +++++++++ 2 files changed, 164 insertions(+) create mode 100644 samples/error-correction/syndrome/Syndrome.cs create mode 100644 samples/error-correction/syndrome/Syndrome.qs diff --git a/samples/error-correction/syndrome/Syndrome.cs b/samples/error-correction/syndrome/Syndrome.cs new file mode 100644 index 000000000000..7979a8ce0212 --- /dev/null +++ b/samples/error-correction/syndrome/Syndrome.cs @@ -0,0 +1,111 @@ +using Microsoft.Quantum.Simulation.Core; +using Microsoft.Quantum.Simulation.Simulators; +using Microsoft.Quantum.Samples.Hardware.Syndrome; +using Microsoft.Quantum.Canon; +using System; +using System.Linq; +using Microsoft.Quantum.Diagnostics; + +namespace Microsoft.Quantum.Samples.Hardware +{ + public class PseudoSyndromeExperiment + { + public class Outcome + { + public bool[] inputs; + public bool[] outputs; + public Pauli[] bases; + public long[] interactions; + } + + public PseudoSyndromeExperiment(uint sample_count, int width, int depth) + { + this.sample_count_ = sample_count; + this.width_ = width; + this.depth_ = depth; + } + + public Outcome[] Run(IOperationFactory sim) + { + var outcomes = new Outcome[this.sample_count_]; + for (int i = 0; i < this.sample_count_; i++) + { + outcomes[i] = Sample(sim); + } + return outcomes; + } + + public Outcome[] Run(IQuantumMachine machine) + { + var outcomes = new Outcome[this.sample_count_]; + for (int i = 0; i < this.sample_count_; i++) + { + outcomes[i] = Sample(machine); + } + return outcomes; + } + + private Outcome Sample(IOperationFactory sim) + { + var indexes = new QArray(MakeRandomIntegerSequence(this.width_, this.depth_)); + var bases = new QArray(MakeRandomPaulis(this.width_)); + var input_values = new QArray(MakeRandomBoolSequence(this.width_)); + var results = SamplePseudoSyndrome.Run(sim, input_values, bases, indexes).Result; + return new Outcome + { + bases = bases.ToArray(), + inputs = input_values.ToArray(), + interactions = indexes.ToArray(), + outputs = (from result in results + select System.Convert.ToBoolean(result.GetValue())).ToArray() + }; + } + + private Outcome Sample(IQuantumMachine machine) + { + var indexes = new QArray(MakeRandomIntegerSequence(this.width_, this.depth_)); + var bases = new QArray(MakeRandomPaulis(this.width_)); + var input_values = new QArray(MakeRandomBoolSequence(this.width_)); + var output = machine.Run, IQArray, IQArray), + IQArray>((input_values, bases, indexes), 1).Result; + var results = output.Result; + return new Outcome + { + bases = bases.ToArray(), + inputs = input_values.ToArray(), + interactions = indexes.ToArray(), + outputs = (from result in results + select System.Convert.ToBoolean(result.GetValue())).ToArray() + }; + } + + private long[] MakeRandomIntegerSequence(int distance, int depth) + { + var random = new System.Random(); + var sequence = new long[depth]; + for (int index = 0; index < sequence.Length; index++) + { + sequence[index] = random.Next(distance); + } + return sequence; + } + + private bool[] MakeRandomBoolSequence(int length) + { + return (from value in MakeRandomIntegerSequence(2, length) + select System.Convert.ToBoolean(value)).ToArray(); + } + + private Pauli[] MakeRandomPaulis(int length) + { + var paulis = new Pauli[]{ Pauli.PauliX, Pauli.PauliY, Pauli.PauliZ }; + return (from value in MakeRandomIntegerSequence(3, length) + select paulis[value]).ToArray(); + } + + private readonly uint sample_count_; + private readonly int width_; + private readonly int depth_; + } +} \ No newline at end of file diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs new file mode 100644 index 000000000000..20b990a0cdef --- /dev/null +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -0,0 +1,53 @@ +namespace Microsoft.Quantum.Samples.Hardware.Syndrome +{ + open Microsoft.Quantum.Math; + open Microsoft.Quantum.Canon; + open Microsoft.Quantum.Intrinsic; + open Microsoft.Quantum.Preparation; + open Microsoft.Quantum.Measurement; + open Microsoft.Quantum.Arrays; + + operation ControlledPauli(basis: Pauli, control: Qubit, target: Qubit): Unit { + if (PauliZ == basis) { + CZ(control, target); + } + if (PauliX == basis) { + CX(control, target); + } + if (PauliY == basis) { + CY(control, target); + } + } + + operation BasisMeasure(basis_qubit: (Pauli, Qubit)): Result { + let (basis, qubit) = basis_qubit; + let result = Measure([basis], [qubit]); + Reset(qubit); + return result; + } + + operation Prepare(qubit: Qubit, value: Bool, basis: Pauli): Unit { + if (value) { + X(qubit); + } + PrepareQubit(basis, qubit); + } + + operation SamplePseudoSyndrome ( + input_values: Bool[], + encoding_bases: Pauli[], + indexes: Int[] + ): Result[] { + using ((block, ancilla) = (Qubit[Length(input_values)], Qubit())) { + for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { + Prepare(qubit, value, basis); + } + H(ancilla); + for (index in indexes) { + ControlledPauli(encoding_bases[index], ancilla, block[index]); + } + Reset(ancilla); + return ForEach(BasisMeasure, Zip(encoding_bases, block)); + } + } +} \ No newline at end of file From c669888574f6c62979715fb6ae0011eb617afca7 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Fri, 7 Aug 2020 00:16:36 +0000 Subject: [PATCH 02/22] add csproj file --- samples/error-correction/syndrome/Syndrome.csproj | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 samples/error-correction/syndrome/Syndrome.csproj diff --git a/samples/error-correction/syndrome/Syndrome.csproj b/samples/error-correction/syndrome/Syndrome.csproj new file mode 100644 index 000000000000..e8f657199fba --- /dev/null +++ b/samples/error-correction/syndrome/Syndrome.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + From 13e65c66a7089a5c9d5e9a08b0c0c69390fdbaca Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Mon, 10 Aug 2020 19:36:26 +0000 Subject: [PATCH 03/22] add ancilla to output --- samples/error-correction/syndrome/Syndrome.qs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 20b990a0cdef..4aee25ce7388 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -37,7 +37,7 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome input_values: Bool[], encoding_bases: Pauli[], indexes: Int[] - ): Result[] { + ): ( Result, Result[] ) { using ((block, ancilla) = (Qubit[Length(input_values)], Qubit())) { for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { Prepare(qubit, value, basis); @@ -46,8 +46,10 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome for (index in indexes) { ControlledPauli(encoding_bases[index], ancilla, block[index]); } + let ancilla_result = Measure([PauliX], [ancilla]); + let data_result = ForEach(BasisMeasure, Zip(encoding_bases, block)); Reset(ancilla); - return ForEach(BasisMeasure, Zip(encoding_bases, block)); + return ( ancilla_result, data_result ); } } } \ No newline at end of file From e2d81e903d064cd6f1869f1d06c67e7269ccc4c4 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Mon, 10 Aug 2020 19:36:57 +0000 Subject: [PATCH 04/22] remove cs file --- samples/error-correction/syndrome/Syndrome.cs | 111 ------------------ 1 file changed, 111 deletions(-) delete mode 100644 samples/error-correction/syndrome/Syndrome.cs diff --git a/samples/error-correction/syndrome/Syndrome.cs b/samples/error-correction/syndrome/Syndrome.cs deleted file mode 100644 index 7979a8ce0212..000000000000 --- a/samples/error-correction/syndrome/Syndrome.cs +++ /dev/null @@ -1,111 +0,0 @@ -using Microsoft.Quantum.Simulation.Core; -using Microsoft.Quantum.Simulation.Simulators; -using Microsoft.Quantum.Samples.Hardware.Syndrome; -using Microsoft.Quantum.Canon; -using System; -using System.Linq; -using Microsoft.Quantum.Diagnostics; - -namespace Microsoft.Quantum.Samples.Hardware -{ - public class PseudoSyndromeExperiment - { - public class Outcome - { - public bool[] inputs; - public bool[] outputs; - public Pauli[] bases; - public long[] interactions; - } - - public PseudoSyndromeExperiment(uint sample_count, int width, int depth) - { - this.sample_count_ = sample_count; - this.width_ = width; - this.depth_ = depth; - } - - public Outcome[] Run(IOperationFactory sim) - { - var outcomes = new Outcome[this.sample_count_]; - for (int i = 0; i < this.sample_count_; i++) - { - outcomes[i] = Sample(sim); - } - return outcomes; - } - - public Outcome[] Run(IQuantumMachine machine) - { - var outcomes = new Outcome[this.sample_count_]; - for (int i = 0; i < this.sample_count_; i++) - { - outcomes[i] = Sample(machine); - } - return outcomes; - } - - private Outcome Sample(IOperationFactory sim) - { - var indexes = new QArray(MakeRandomIntegerSequence(this.width_, this.depth_)); - var bases = new QArray(MakeRandomPaulis(this.width_)); - var input_values = new QArray(MakeRandomBoolSequence(this.width_)); - var results = SamplePseudoSyndrome.Run(sim, input_values, bases, indexes).Result; - return new Outcome - { - bases = bases.ToArray(), - inputs = input_values.ToArray(), - interactions = indexes.ToArray(), - outputs = (from result in results - select System.Convert.ToBoolean(result.GetValue())).ToArray() - }; - } - - private Outcome Sample(IQuantumMachine machine) - { - var indexes = new QArray(MakeRandomIntegerSequence(this.width_, this.depth_)); - var bases = new QArray(MakeRandomPaulis(this.width_)); - var input_values = new QArray(MakeRandomBoolSequence(this.width_)); - var output = machine.Run, IQArray, IQArray), - IQArray>((input_values, bases, indexes), 1).Result; - var results = output.Result; - return new Outcome - { - bases = bases.ToArray(), - inputs = input_values.ToArray(), - interactions = indexes.ToArray(), - outputs = (from result in results - select System.Convert.ToBoolean(result.GetValue())).ToArray() - }; - } - - private long[] MakeRandomIntegerSequence(int distance, int depth) - { - var random = new System.Random(); - var sequence = new long[depth]; - for (int index = 0; index < sequence.Length; index++) - { - sequence[index] = random.Next(distance); - } - return sequence; - } - - private bool[] MakeRandomBoolSequence(int length) - { - return (from value in MakeRandomIntegerSequence(2, length) - select System.Convert.ToBoolean(value)).ToArray(); - } - - private Pauli[] MakeRandomPaulis(int length) - { - var paulis = new Pauli[]{ Pauli.PauliX, Pauli.PauliY, Pauli.PauliZ }; - return (from value in MakeRandomIntegerSequence(3, length) - select paulis[value]).ToArray(); - } - - private readonly uint sample_count_; - private readonly int width_; - private readonly int depth_; - } -} \ No newline at end of file From 36561253fecae042452786d24deef8ae4ea1a817 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Mon, 10 Aug 2020 19:54:26 +0000 Subject: [PATCH 05/22] add Python script for running Syndrome example --- samples/error-correction/syndrome/syndrome.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 samples/error-correction/syndrome/syndrome.py diff --git a/samples/error-correction/syndrome/syndrome.py b/samples/error-correction/syndrome/syndrome.py new file mode 100644 index 000000000000..dcebd252e840 --- /dev/null +++ b/samples/error-correction/syndrome/syndrome.py @@ -0,0 +1,36 @@ +""" +Script for running Pseudo-syndrome example as defined in Syndrome.qs. +This script takes the number of data qubits as input. + +Example usage: + python syndrome.py -q 5 +""" + +import numpy as np +import qsharp +import argparse + +from Microsoft.Quantum.Samples.Hardware.Syndrome import SamplePseudoSyndrome + +parser = argparse.ArgumentParser( + prog="PseudoSyndrome", + description="Program for running a PseudoSyndrome circuit that detects random errors on qubits in gate sequence." +) +parser.add_argument("-q", "--qubits", type=int, default=1) + +if __name__ == "__main__": + args = parser.parse_args() + paulis = ["PauliX", "PauliY", "PauliZ"] + indexes = list(range(args.qubits)) + np.random.shuffle(indexes) + input_values = [np.random.rand() > .5 for n in range(args.qubits)] + encoding_bases = [np.random.choice(paulis) for n in range(args.qubits)] + result = SamplePseudoSyndrome.simulate( + input_values=input_values, + encoding_bases=encoding_bases, + indexes=indexes) + ancilla, data = result + + print(f"Inputs: {[int(val) for val in input_values]}, Bases: {encoding_bases}, Indexes: {indexes}") + print(f"Ancilla: {ancilla}") + print(f"Data qubits: {data}") From 7afaee20e8fb90e390d73848c7ae18fd5c5377c6 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Mon, 10 Aug 2020 20:36:40 +0000 Subject: [PATCH 06/22] add copyright, docs --- samples/error-correction/syndrome/Syndrome.qs | 51 ++++++++++++++++--- samples/error-correction/syndrome/syndrome.py | 2 + 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 4aee25ce7388..cf5228ae8f10 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -1,3 +1,5 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. namespace Microsoft.Quantum.Samples.Hardware.Syndrome { open Microsoft.Quantum.Math; @@ -6,7 +8,18 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome open Microsoft.Quantum.Preparation; open Microsoft.Quantum.Measurement; open Microsoft.Quantum.Arrays; + open Microsoft.Quantum.Core; + /// # Summary + /// Apply a controlled Pauli operation for a given basis and control/target qubits + /// + /// # Input + /// ## basis + /// Pauli basis in which to apply the controlled pauli + /// ## control + /// Control qubit + /// ## target + /// Target qubit operation ControlledPauli(basis: Pauli, control: Qubit, target: Qubit): Unit { if (PauliZ == basis) { CZ(control, target); @@ -19,6 +32,16 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome } } + /// # Summary + /// Measure qubit in a given basis and return the result + /// + /// # Input + /// ## basis_qubit + /// Tuple of the Pauli basis and qubit to measure + /// + /// # Output + /// ## result + /// Measurement result operation BasisMeasure(basis_qubit: (Pauli, Qubit)): Result { let (basis, qubit) = basis_qubit; let result = Measure([basis], [qubit]); @@ -33,23 +56,39 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome PrepareQubit(basis, qubit); } + /// # Summary + /// Creates a Pseudo Syndrome by using an auxiliary qubit. + /// This algorithm relies on several controlled pauli operations. When there is + /// no error introduced after state preparation, the circuit is trivial and no + /// change is measured on the auxiliary qubit. However, if there are miscellaneous + /// rotations on the data qubits due to noise, due to phase kickback the auxiliary + /// qubit will gain a small phase shift. By measuring the auxiliary qubit in the + /// X basis we then project that phase difference onto the measurement basis. + /// + /// # Input + /// ## input_values + /// List of boolean values for data qubits + /// ## encoding_bases + /// List of Pauli bases + /// ## indexes + /// List of indexes on which to apply controlled pauli operators operation SamplePseudoSyndrome ( input_values: Bool[], encoding_bases: Pauli[], indexes: Int[] ): ( Result, Result[] ) { - using ((block, ancilla) = (Qubit[Length(input_values)], Qubit())) { + using ((block, auxiliary) = (Qubit[Length(input_values)], Qubit())) { for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { Prepare(qubit, value, basis); } - H(ancilla); + H(auxiliary); for (index in indexes) { - ControlledPauli(encoding_bases[index], ancilla, block[index]); + ControlledPauli(encoding_bases[index], auxiliary, block[index]); } - let ancilla_result = Measure([PauliX], [ancilla]); + let auxiliary_result = Measure([PauliX], [auxiliary]); let data_result = ForEach(BasisMeasure, Zip(encoding_bases, block)); - Reset(ancilla); - return ( ancilla_result, data_result ); + Reset(auxiliary); + return ( auxiliary_result, data_result ); } } } \ No newline at end of file diff --git a/samples/error-correction/syndrome/syndrome.py b/samples/error-correction/syndrome/syndrome.py index dcebd252e840..8d9b7be28e0a 100644 --- a/samples/error-correction/syndrome/syndrome.py +++ b/samples/error-correction/syndrome/syndrome.py @@ -1,3 +1,5 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. """ Script for running Pseudo-syndrome example as defined in Syndrome.qs. This script takes the number of data qubits as input. From c201059e52826b8349dfcc8ba2dd542e633dabf7 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Mon, 10 Aug 2020 20:59:54 +0000 Subject: [PATCH 07/22] replace tab indents by spaces --- samples/error-correction/syndrome/Syndrome.qs | 160 +++++++++--------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index cf5228ae8f10..f3bc3e33d6dd 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -7,88 +7,88 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Preparation; open Microsoft.Quantum.Measurement; - open Microsoft.Quantum.Arrays; - open Microsoft.Quantum.Core; + open Microsoft.Quantum.Arrays; + open Microsoft.Quantum.Core; - /// # Summary - /// Apply a controlled Pauli operation for a given basis and control/target qubits - /// - /// # Input - /// ## basis - /// Pauli basis in which to apply the controlled pauli - /// ## control - /// Control qubit - /// ## target - /// Target qubit - operation ControlledPauli(basis: Pauli, control: Qubit, target: Qubit): Unit { - if (PauliZ == basis) { - CZ(control, target); - } - if (PauliX == basis) { - CX(control, target); - } - if (PauliY == basis) { - CY(control, target); - } - } + /// # Summary + /// Apply a controlled Pauli operation for a given basis and control/target qubits + /// + /// # Input + /// ## basis + /// Pauli basis in which to apply the controlled pauli + /// ## control + /// Control qubit + /// ## target + /// Target qubit + operation ControlledPauli(basis: Pauli, control: Qubit, target: Qubit): Unit { + if (PauliZ == basis) { + CZ(control, target); + } + if (PauliX == basis) { + CX(control, target); + } + if (PauliY == basis) { + CY(control, target); + } + } - /// # Summary - /// Measure qubit in a given basis and return the result - /// - /// # Input - /// ## basis_qubit - /// Tuple of the Pauli basis and qubit to measure - /// - /// # Output - /// ## result - /// Measurement result - operation BasisMeasure(basis_qubit: (Pauli, Qubit)): Result { - let (basis, qubit) = basis_qubit; - let result = Measure([basis], [qubit]); - Reset(qubit); - return result; - } + /// # Summary + /// Measure qubit in a given basis and return the result + /// + /// # Input + /// ## basis_qubit + /// Tuple of the Pauli basis and qubit to measure + /// + /// # Output + /// ## result + /// Measurement result + operation BasisMeasure(basis_qubit: (Pauli, Qubit)): Result { + let (basis, qubit) = basis_qubit; + let result = Measure([basis], [qubit]); + Reset(qubit); + return result; + } - operation Prepare(qubit: Qubit, value: Bool, basis: Pauli): Unit { - if (value) { - X(qubit); - } - PrepareQubit(basis, qubit); - } + operation Prepare(qubit: Qubit, value: Bool, basis: Pauli): Unit { + if (value) { + X(qubit); + } + PrepareQubit(basis, qubit); + } - /// # Summary - /// Creates a Pseudo Syndrome by using an auxiliary qubit. - /// This algorithm relies on several controlled pauli operations. When there is - /// no error introduced after state preparation, the circuit is trivial and no - /// change is measured on the auxiliary qubit. However, if there are miscellaneous - /// rotations on the data qubits due to noise, due to phase kickback the auxiliary - /// qubit will gain a small phase shift. By measuring the auxiliary qubit in the - /// X basis we then project that phase difference onto the measurement basis. - /// - /// # Input - /// ## input_values - /// List of boolean values for data qubits - /// ## encoding_bases - /// List of Pauli bases - /// ## indexes - /// List of indexes on which to apply controlled pauli operators - operation SamplePseudoSyndrome ( - input_values: Bool[], - encoding_bases: Pauli[], - indexes: Int[] - ): ( Result, Result[] ) { - using ((block, auxiliary) = (Qubit[Length(input_values)], Qubit())) { - for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { - Prepare(qubit, value, basis); - } - H(auxiliary); - for (index in indexes) { - ControlledPauli(encoding_bases[index], auxiliary, block[index]); - } - let auxiliary_result = Measure([PauliX], [auxiliary]); - let data_result = ForEach(BasisMeasure, Zip(encoding_bases, block)); - Reset(auxiliary); - return ( auxiliary_result, data_result ); - } - } + /// # Summary + /// Creates a Pseudo Syndrome by using an auxiliary qubit. + /// This algorithm relies on several controlled pauli operations. When there is + /// no error introduced after state preparation, the circuit is trivial and no + /// change is measured on the auxiliary qubit. However, if there are miscellaneous + /// rotations on the data qubits due to noise, due to phase kickback the auxiliary + /// qubit will gain a small phase shift. By measuring the auxiliary qubit in the + /// X basis we then project that phase difference onto the measurement basis. + /// + /// # Input + /// ## input_values + /// List of boolean values for data qubits + /// ## encoding_bases + /// List of Pauli bases + /// ## indexes + /// List of indexes on which to apply controlled pauli operators + operation SamplePseudoSyndrome ( + input_values: Bool[], + encoding_bases: Pauli[], + indexes: Int[] + ): ( Result, Result[] ) { + using ((block, auxiliary) = (Qubit[Length(input_values)], Qubit())) { + for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { + Prepare(qubit, value, basis); + } + H(auxiliary); + for (index in indexes) { + ControlledPauli(encoding_bases[index], auxiliary, block[index]); + } + let auxiliary_result = Measure([PauliX], [auxiliary]); + let data_result = ForEach(BasisMeasure, Zip(encoding_bases, block)); + Reset(auxiliary); + return ( auxiliary_result, data_result ); + } + } } \ No newline at end of file From d1b89ffd2e5fb52bbda69429a812f804b87d73f2 Mon Sep 17 00:00:00 2001 From: Guen P Date: Mon, 10 Aug 2020 17:16:40 -0700 Subject: [PATCH 08/22] Update samples/error-correction/syndrome/Syndrome.csproj Make library project instead of executable project Co-authored-by: Ryan Shaffer --- samples/error-correction/syndrome/Syndrome.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.csproj b/samples/error-correction/syndrome/Syndrome.csproj index e8f657199fba..b1406ca17e9d 100644 --- a/samples/error-correction/syndrome/Syndrome.csproj +++ b/samples/error-correction/syndrome/Syndrome.csproj @@ -1,8 +1,7 @@ - Exe - netcoreapp3.1 + netstandard2.1 From 3bcf9e6cdb845bcf6c62f7150bf456fcd11be2c8 Mon Sep 17 00:00:00 2001 From: Guen P Date: Tue, 11 Aug 2020 13:05:52 -0700 Subject: [PATCH 09/22] Apply suggestions from code review Formatting, docstring Co-authored-by: Chris Granade --- samples/error-correction/syndrome/Syndrome.qs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index f3bc3e33d6dd..102394ef88b5 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -namespace Microsoft.Quantum.Samples.Hardware.Syndrome -{ - open Microsoft.Quantum.Math; +namespace Microsoft.Quantum.Samples.Hardware.Syndrome { open Microsoft.Quantum.Canon; open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Preparation; @@ -15,7 +13,7 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome /// /// # Input /// ## basis - /// Pauli basis in which to apply the controlled pauli + /// Basis in which to apply the controlled Pauli operation. /// ## control /// Control qubit /// ## target @@ -91,4 +89,4 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome return ( auxiliary_result, data_result ); } } -} \ No newline at end of file +} From 4896c041b10527fb4f878b5e27ebe0399fbb149b Mon Sep 17 00:00:00 2001 From: Guen P Date: Tue, 11 Aug 2020 14:30:13 -0700 Subject: [PATCH 10/22] Apply suggestions from code review docstring Co-authored-by: Chris Granade --- samples/error-correction/syndrome/Syndrome.qs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 102394ef88b5..1c4990c79464 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -56,7 +56,7 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome { /// # Summary /// Creates a Pseudo Syndrome by using an auxiliary qubit. - /// This algorithm relies on several controlled pauli operations. When there is + /// This algorithm relies on several controlled Pauli operations. When there is /// no error introduced after state preparation, the circuit is trivial and no /// change is measured on the auxiliary qubit. However, if there are miscellaneous /// rotations on the data qubits due to noise, due to phase kickback the auxiliary From d95129f3019fdbbb874fd908e12c816569e5b942 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Tue, 11 Aug 2020 01:30:32 +0000 Subject: [PATCH 11/22] rename indexes to qubit_indices, check that arrays are of same length, text formatting --- samples/error-correction/syndrome/Syndrome.qs | 33 +++++++++++++------ samples/error-correction/syndrome/syndrome.py | 13 +++++--- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 1c4990c79464..bb65d88a2ae1 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -7,6 +7,8 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome { open Microsoft.Quantum.Measurement; open Microsoft.Quantum.Arrays; open Microsoft.Quantum.Core; + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.Convert; /// # Summary /// Apply a controlled Pauli operation for a given basis and control/target qubits @@ -56,31 +58,42 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome { /// # Summary /// Creates a Pseudo Syndrome by using an auxiliary qubit. - /// This algorithm relies on several controlled Pauli operations. When there is - /// no error introduced after state preparation, the circuit is trivial and no - /// change is measured on the auxiliary qubit. However, if there are miscellaneous - /// rotations on the data qubits due to noise, due to phase kickback the auxiliary - /// qubit will gain a small phase shift. By measuring the auxiliary qubit in the - /// X basis we then project that phase difference onto the measurement basis. + /// This algorithm relies on several controlled Pauli operations. When + /// there is no error introduced after state preparation, the circuit is + /// trivial and no change is measured on the auxiliary qubit. However, if + /// there are miscellaneous rotations on the data qubits due to noise, due + /// to phase kickback the auxiliary qubit will gain a small phase shift. + /// By measuring the auxiliary qubit in the X basis we then project that + /// phase difference onto the measurement basis. /// /// # Input /// ## input_values /// List of boolean values for data qubits /// ## encoding_bases /// List of Pauli bases - /// ## indexes - /// List of indexes on which to apply controlled pauli operators + /// ## qubit_indices + /// List of qubit indices on which to apply controlled pauli operators operation SamplePseudoSyndrome ( input_values: Bool[], encoding_bases: Pauli[], - indexes: Int[] + qubit_indices: Int[] ): ( Result, Result[] ) { + // Check that input lists are of equal length + if ((Length(input_values) != Length(encoding_bases)) + or (Length(input_values) != Length(qubit_indices))) { + fail "Lengths of input values, encoding bases and qubit_indices must be + equal. Found lengths: " + + IntAsString(Length(input_values)) + ", " + + IntAsString(Length(encoding_bases)) + ", " + + IntAsString(Length(qubit_indices)) + "."; + } + using ((block, auxiliary) = (Qubit[Length(input_values)], Qubit())) { for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { Prepare(qubit, value, basis); } H(auxiliary); - for (index in indexes) { + for (index in qubit_indices) { ControlledPauli(encoding_bases[index], auxiliary, block[index]); } let auxiliary_result = Measure([PauliX], [auxiliary]); diff --git a/samples/error-correction/syndrome/syndrome.py b/samples/error-correction/syndrome/syndrome.py index 8d9b7be28e0a..7961e388425f 100644 --- a/samples/error-correction/syndrome/syndrome.py +++ b/samples/error-correction/syndrome/syndrome.py @@ -16,23 +16,26 @@ parser = argparse.ArgumentParser( prog="PseudoSyndrome", - description="Program for running a PseudoSyndrome circuit that detects random errors on qubits in gate sequence." + description="Program for running a PseudoSyndrome circuit that detects \ + random errors on qubits in gate sequence." ) parser.add_argument("-q", "--qubits", type=int, default=1) if __name__ == "__main__": args = parser.parse_args() paulis = ["PauliX", "PauliY", "PauliZ"] - indexes = list(range(args.qubits)) - np.random.shuffle(indexes) + qubit_indices = list(range(args.qubits)) + np.random.shuffle(qubit_indices) input_values = [np.random.rand() > .5 for n in range(args.qubits)] encoding_bases = [np.random.choice(paulis) for n in range(args.qubits)] result = SamplePseudoSyndrome.simulate( input_values=input_values, encoding_bases=encoding_bases, - indexes=indexes) + qubit_indices=qubit_indices) ancilla, data = result - print(f"Inputs: {[int(val) for val in input_values]}, Bases: {encoding_bases}, Indexes: {indexes}") + print(f"Inputs: {[int(val) for val in input_values]}\ + \nBases: {encoding_bases}\ + \nQubit indices: {qubit_indices}") print(f"Ancilla: {ancilla}") print(f"Data qubits: {data}") From 5d5abf907085a1d77c0dce13a908c16e2d759a97 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Wed, 12 Aug 2020 00:03:01 +0000 Subject: [PATCH 12/22] Respond to code review comments --- .../error-correction/syndrome/Syndrome.csproj | 2 +- samples/error-correction/syndrome/Syndrome.qs | 72 ++++++++++--------- samples/error-correction/syndrome/syndrome.py | 6 +- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.csproj b/samples/error-correction/syndrome/Syndrome.csproj index b1406ca17e9d..cffd30f1fd06 100644 --- a/samples/error-correction/syndrome/Syndrome.csproj +++ b/samples/error-correction/syndrome/Syndrome.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index bb65d88a2ae1..135cb6c80045 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -namespace Microsoft.Quantum.Samples.Hardware.Syndrome { +namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome +{ + open Microsoft.Quantum.Math; open Microsoft.Quantum.Canon; open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Preparation; @@ -10,46 +12,34 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome { open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.Convert; - /// # Summary - /// Apply a controlled Pauli operation for a given basis and control/target qubits - /// - /// # Input - /// ## basis - /// Basis in which to apply the controlled Pauli operation. - /// ## control - /// Control qubit - /// ## target - /// Target qubit - operation ControlledPauli(basis: Pauli, control: Qubit, target: Qubit): Unit { - if (PauliZ == basis) { - CZ(control, target); - } - if (PauliX == basis) { - CX(control, target); - } - if (PauliY == basis) { - CY(control, target); - } - } - /// # Summary /// Measure qubit in a given basis and return the result /// /// # Input - /// ## basis_qubit - /// Tuple of the Pauli basis and qubit to measure + /// ## basis + /// Pauli basis in which to perform measurement + /// ## qubit + /// Qubit to measure /// /// # Output /// ## result /// Measurement result - operation BasisMeasure(basis_qubit: (Pauli, Qubit)): Result { - let (basis, qubit) = basis_qubit; + operation BasisMeasure(basis: Pauli, qubit: Qubit): Result { let result = Measure([basis], [qubit]); - Reset(qubit); return result; } - operation Prepare(qubit: Qubit, value: Bool, basis: Pauli): Unit { + /// # Summary + /// Prepare qubit in a given basis. Optionally flip the qubit if the value is True (One). + /// + /// # Input + /// ## qubit + /// Qubit to prepare + /// ## value + /// Value to prepare the qubit in (True for One, False for Zero) + /// ## basis + /// Basis to prepare the qubit in + operation PrepareInBasis(qubit: Qubit, value: Bool, basis: Pauli): Unit { if (value) { X(qubit); } @@ -68,11 +58,19 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome { /// /// # Input /// ## input_values - /// List of boolean values for data qubits + /// Array of Boolean input values for data qubits. /// ## encoding_bases - /// List of Pauli bases + /// Array of Pauli bases to encode errors in for controlled Pauli operations on. + /// The length of this array needs to be the same as + /// input_qubits. /// ## qubit_indices - /// List of qubit indices on which to apply controlled pauli operators + /// List of qubit indices on which to apply controlled Pauli operators. + /// This determines the order in which the controlled Pauli's are applied. + /// The length of this array needs to be the same as input_qubits. + /// + /// # Output + /// ## (auxiliary_result, data_result) + /// Tuple of the measurement results of the auxiliary qubit and data qubits. operation SamplePseudoSyndrome ( input_values: Bool[], encoding_bases: Pauli[], @@ -90,14 +88,18 @@ namespace Microsoft.Quantum.Samples.Hardware.Syndrome { using ((block, auxiliary) = (Qubit[Length(input_values)], Qubit())) { for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { - Prepare(qubit, value, basis); + PrepareInBasis(qubit, value, basis); } H(auxiliary); - for (index in qubit_indices) { - ControlledPauli(encoding_bases[index], auxiliary, block[index]); + // Apply Controlled Pauli's to data qubits, resulting in a phase kickback + /// on the auxiliary qubit + for ((qubit, basis) in Zip(block, encoding_bases)) { + Controlled ApplyPauli([auxiliary], ([basis], [qubit])); } let auxiliary_result = Measure([PauliX], [auxiliary]); let data_result = ForEach(BasisMeasure, Zip(encoding_bases, block)); + // Reset qubits - optional, only for QDK version < 0.12 + ResetAll(block); Reset(auxiliary); return ( auxiliary_result, data_result ); } diff --git a/samples/error-correction/syndrome/syndrome.py b/samples/error-correction/syndrome/syndrome.py index 7961e388425f..2ebc54d8e60f 100644 --- a/samples/error-correction/syndrome/syndrome.py +++ b/samples/error-correction/syndrome/syndrome.py @@ -12,7 +12,7 @@ import qsharp import argparse -from Microsoft.Quantum.Samples.Hardware.Syndrome import SamplePseudoSyndrome +from Microsoft.Quantum.Samples.ErrorCorrection.Syndrome import SamplePseudoSyndrome parser = argparse.ArgumentParser( prog="PseudoSyndrome", @@ -32,10 +32,10 @@ input_values=input_values, encoding_bases=encoding_bases, qubit_indices=qubit_indices) - ancilla, data = result + auxiliary, data = result print(f"Inputs: {[int(val) for val in input_values]}\ \nBases: {encoding_bases}\ \nQubit indices: {qubit_indices}") - print(f"Ancilla: {ancilla}") + print(f"Auxiliary: {auxiliary}") print(f"Data qubits: {data}") From 9f8ebbf38c8558b537d423630ccc5a902c6beafb Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Wed, 12 Aug 2020 00:17:43 +0000 Subject: [PATCH 13/22] snake to camel case --- samples/error-correction/syndrome/Syndrome.qs | 42 +++++++++---------- samples/error-correction/syndrome/syndrome.py | 8 ++-- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 135cb6c80045..d1848b24bd08 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -57,51 +57,51 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome /// phase difference onto the measurement basis. /// /// # Input - /// ## input_values + /// ## inputValues /// Array of Boolean input values for data qubits. - /// ## encoding_bases + /// ## encodingBases /// Array of Pauli bases to encode errors in for controlled Pauli operations on. /// The length of this array needs to be the same as - /// input_qubits. - /// ## qubit_indices + /// inputValues. + /// ## qubitIndices /// List of qubit indices on which to apply controlled Pauli operators. /// This determines the order in which the controlled Pauli's are applied. - /// The length of this array needs to be the same as input_qubits. + /// The length of this array needs to be the same as inputValues. /// /// # Output - /// ## (auxiliary_result, data_result) + /// ## (auxiliaryResult, dataResult) /// Tuple of the measurement results of the auxiliary qubit and data qubits. operation SamplePseudoSyndrome ( - input_values: Bool[], - encoding_bases: Pauli[], - qubit_indices: Int[] + inputValues: Bool[], + encodingBases: Pauli[], + qubitIndices: Int[] ): ( Result, Result[] ) { // Check that input lists are of equal length - if ((Length(input_values) != Length(encoding_bases)) - or (Length(input_values) != Length(qubit_indices))) { - fail "Lengths of input values, encoding bases and qubit_indices must be + if ((Length(inputValues) != Length(encodingBases)) + or (Length(inputValues) != Length(qubitIndices))) { + fail "Lengths of input values, encoding bases and qubitIndices must be equal. Found lengths: " - + IntAsString(Length(input_values)) + ", " - + IntAsString(Length(encoding_bases)) + ", " - + IntAsString(Length(qubit_indices)) + "."; + + IntAsString(Length(inputValues)) + ", " + + IntAsString(Length(encodingBases)) + ", " + + IntAsString(Length(qubitIndices)) + "."; } - using ((block, auxiliary) = (Qubit[Length(input_values)], Qubit())) { - for ((qubit, value, basis) in Zip3(block, input_values, encoding_bases)) { + using ((block, auxiliary) = (Qubit[Length(inputValues)], Qubit())) { + for ((qubit, value, basis) in Zip3(block, inputValues, encodingBases)) { PrepareInBasis(qubit, value, basis); } H(auxiliary); // Apply Controlled Pauli's to data qubits, resulting in a phase kickback /// on the auxiliary qubit - for ((qubit, basis) in Zip(block, encoding_bases)) { + for ((qubit, basis) in Zip(block, encodingBases)) { Controlled ApplyPauli([auxiliary], ([basis], [qubit])); } - let auxiliary_result = Measure([PauliX], [auxiliary]); - let data_result = ForEach(BasisMeasure, Zip(encoding_bases, block)); + let auxiliaryResult = Measure([PauliX], [auxiliary]); + let dataResult = ForEach(BasisMeasure, Zip(encodingBases, block)); // Reset qubits - optional, only for QDK version < 0.12 ResetAll(block); Reset(auxiliary); - return ( auxiliary_result, data_result ); + return ( auxiliaryResult, dataResult ); } } } diff --git a/samples/error-correction/syndrome/syndrome.py b/samples/error-correction/syndrome/syndrome.py index 2ebc54d8e60f..621c4c3763e2 100644 --- a/samples/error-correction/syndrome/syndrome.py +++ b/samples/error-correction/syndrome/syndrome.py @@ -23,15 +23,17 @@ if __name__ == "__main__": args = parser.parse_args() + # To be refactored to qsharp.Pauli + # (See IQSharp ticket https://github.com/microsoft/iqsharp/issues/256) paulis = ["PauliX", "PauliY", "PauliZ"] qubit_indices = list(range(args.qubits)) np.random.shuffle(qubit_indices) input_values = [np.random.rand() > .5 for n in range(args.qubits)] encoding_bases = [np.random.choice(paulis) for n in range(args.qubits)] result = SamplePseudoSyndrome.simulate( - input_values=input_values, - encoding_bases=encoding_bases, - qubit_indices=qubit_indices) + inputValues=input_values, + encodingBases=encoding_bases, + qubitIndices=qubit_indices) auxiliary, data = result print(f"Inputs: {[int(val) for val in input_values]}\ From 9610488fa0f52521501ec04fcd1530135b751193 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Wed, 12 Aug 2020 00:20:34 +0000 Subject: [PATCH 14/22] remove unused namespaces --- samples/error-correction/syndrome/Syndrome.qs | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index d1848b24bd08..0bc0cc0e169f 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -2,13 +2,11 @@ // Licensed under the MIT License. namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { - open Microsoft.Quantum.Math; open Microsoft.Quantum.Canon; open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Preparation; open Microsoft.Quantum.Measurement; open Microsoft.Quantum.Arrays; - open Microsoft.Quantum.Core; open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.Convert; From 8807d138c20006f3183fc3dd78bfe94a2e5b48f9 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Wed, 12 Aug 2020 22:02:28 +0000 Subject: [PATCH 15/22] Add Syndrome to README --- samples/error-correction/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/error-correction/README.md b/samples/error-correction/README.md index 6d1fe80436b0..1c78ac476ef7 100644 --- a/samples/error-correction/README.md +++ b/samples/error-correction/README.md @@ -4,4 +4,6 @@ These samples show how to work with quantum error correcting codes in Q# program - **[Bit-flip Code](./bit-flip-code)**: This sample describes a simple quantum code that encodes 1 qubit into 3 qubits and protects against a single bit-flip error. - \ No newline at end of file + +- **[Syndrome](./syndrome)**: + This sample implements a simple syndrome that can be used to detect errors in quantum algorithms executed on quantum hardware. From a2364946ae82d2af268b3a8b511137187da33fc8 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Thu, 13 Aug 2020 21:04:16 +0000 Subject: [PATCH 16/22] code review suggestion: formatting, reoder inputs for PrepareInBasis --- samples/error-correction/syndrome/Syndrome.qs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 0bc0cc0e169f..528e9b02d856 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome -{ +namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { open Microsoft.Quantum.Canon; open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Preparation; @@ -28,16 +27,17 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome } /// # Summary - /// Prepare qubit in a given basis. Optionally flip the qubit if the value is True (One). + /// Prepare qubit in a given basis. Optionally flip the qubit if the value + /// is True (One). /// /// # Input + /// ## basis + /// Basis to prepare the qubit in /// ## qubit /// Qubit to prepare /// ## value /// Value to prepare the qubit in (True for One, False for Zero) - /// ## basis - /// Basis to prepare the qubit in - operation PrepareInBasis(qubit: Qubit, value: Bool, basis: Pauli): Unit { + operation PrepareInBasis(basis: Pauli, qubit: Qubit, value: Bool): Unit { if (value) { X(qubit); } @@ -77,16 +77,14 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome // Check that input lists are of equal length if ((Length(inputValues) != Length(encodingBases)) or (Length(inputValues) != Length(qubitIndices))) { - fail "Lengths of input values, encoding bases and qubitIndices must be - equal. Found lengths: " - + IntAsString(Length(inputValues)) + ", " - + IntAsString(Length(encodingBases)) + ", " - + IntAsString(Length(qubitIndices)) + "."; + fail $"Lengths of input values, encoding bases and qubitIndices must be + equal. Found lengths: + {Length(inputValues)}, {Length(encodingBases)}, {Length(qubitIndices)}"; } using ((block, auxiliary) = (Qubit[Length(inputValues)], Qubit())) { for ((qubit, value, basis) in Zip3(block, inputValues, encodingBases)) { - PrepareInBasis(qubit, value, basis); + PrepareInBasis(basis, qubit, value); } H(auxiliary); // Apply Controlled Pauli's to data qubits, resulting in a phase kickback From f3ebd15a3333677915b5dbdf180ec718d34d8237 Mon Sep 17 00:00:00 2001 From: Guen P Date: Thu, 13 Aug 2020 14:05:58 -0700 Subject: [PATCH 17/22] Apply suggestions from code review docs, formatting Co-authored-by: Chris Granade --- samples/error-correction/syndrome/Syndrome.qs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 528e9b02d856..f5762b72d190 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -21,7 +21,7 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { /// # Output /// ## result /// Measurement result - operation BasisMeasure(basis: Pauli, qubit: Qubit): Result { + operation BasisMeasure(basis : Pauli, qubit : Qubit): Result { let result = Measure([basis], [qubit]); return result; } @@ -87,7 +87,7 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { PrepareInBasis(basis, qubit, value); } H(auxiliary); - // Apply Controlled Pauli's to data qubits, resulting in a phase kickback + // Apply Controlled Pauli operations to data qubits, resulting in a phase kickback /// on the auxiliary qubit for ((qubit, basis) in Zip(block, encodingBases)) { Controlled ApplyPauli([auxiliary], ([basis], [qubit])); From f52f70ac1385674d599983d8122cd8299ff4ceb2 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Thu, 13 Aug 2020 21:09:35 +0000 Subject: [PATCH 18/22] formatting --- samples/error-correction/syndrome/Syndrome.qs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index f5762b72d190..353f925464d1 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -37,7 +37,7 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { /// Qubit to prepare /// ## value /// Value to prepare the qubit in (True for One, False for Zero) - operation PrepareInBasis(basis: Pauli, qubit: Qubit, value: Bool): Unit { + operation PrepareInBasis(basis : Pauli, qubit : Qubit, value : Bool): Unit { if (value) { X(qubit); } @@ -70,9 +70,9 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { /// ## (auxiliaryResult, dataResult) /// Tuple of the measurement results of the auxiliary qubit and data qubits. operation SamplePseudoSyndrome ( - inputValues: Bool[], - encodingBases: Pauli[], - qubitIndices: Int[] + inputValues : Bool[], + encodingBases : Pauli[], + qubitIndices : Int[] ): ( Result, Result[] ) { // Check that input lists are of equal length if ((Length(inputValues) != Length(encodingBases)) From 7eddc3ff7037d1d3fea300f7ca0b10d76ebf1223 Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Thu, 13 Aug 2020 22:46:32 +0000 Subject: [PATCH 19/22] Add README --- samples/error-correction/syndrome/README.md | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 samples/error-correction/syndrome/README.md diff --git a/samples/error-correction/syndrome/README.md b/samples/error-correction/syndrome/README.md new file mode 100644 index 000000000000..a03155f19cf2 --- /dev/null +++ b/samples/error-correction/syndrome/README.md @@ -0,0 +1,39 @@ +--- +page_type: sample +languages: +- qsharp +products: +- qdk +description: "This sample uses the Q# standard libraries to implement a Syndrome for detecting errors in a given number of data qubits." +urlFragment: syndrome +--- + + +# Syndrome Sample + +This sample demonstrates how to create a partially implemented Syndrome for detecting errors generated in noisy hardware. The sample shows how this is implemented using Q# standard libraries. Currently, it does not simulate realistic noise so the resulting unitary operator is trivial, however, it is useful to demonstrate the concept of a Syndrome. + +The algorithm used is described in [Surface codes: Towards practical large-scale quantum computation](https://arxiv.org/abs/1208.0928) by Fowler et al. In particular, this example implements the circuit shown in Figure 1 c. in this paper. + +The circuit uses _N data qubits_, where _N_ is a number given as input to the script, plus one _auxiliary_ qubit. The principle of the circuit is as follows. First, we prepare our data qubits in random states of a set of random bases, in order to simulate a noisy process. The circuit then propagates these errors to the auxiliary qubit using the principle of _phase kickback_. Phase kickback works by encoding pieces of a quantum algorithm into the global phase of an extra auxiliary qubit. + +We start by preparing an auxiliary qubit into the superposition state by applying a _H_ gate, to change to the _X_ computational basis. Subsequently, we apply controlled Pauli operators to each of the data qubits, using the auxiliary qubit as control. The goal is to create a global phase shift on the auxiliary qubit, that will depend on the state of the basis qubits. After the circuit runs, the auxiliary qubit is measured in the _X_-basis, which reveals its phase information. + +## Prerequisites + +- The Microsoft [Quantum Development Kit](https://docs.microsoft.com/quantum/install-guide/). + +## Running the Sample + +To run the sample, run `python syndrome.py --qubits `", where `` is replaced by the number of qubits. For more information, run `python syndrome.py --help`. + +## Manifest + +- [Syndrome.qs](https://github.com/microsoft/Quantum/blob/master/samples/error-correction/syndrome/Syndrome.qs): Q# code implementing quantum operations for this sample. +- [syndrome.py](https://github.com/microsoft/Quantum/blob/master/samples/error-correction/syndrome/syndrome.py): Python script to interact with and print out results of the Q# operations for this sample for a given number of data qubits. Example usage: `python syndrome.py --qubits 5`. +- [Syndrome.csproj](https://github.com/microsoft/Quantum/blob/master/samples/error-correction/syndrome/Syndrome.csproj): Main Q# project for the sample. + +## Further resources + +- [Error correction library concepts](https://docs.microsoft.com/quantum/libraries/standard/error-correction) +- [Pauli measurements](https://docs.microsoft.com/en-us/quantum/concepts/pauli-measurements) From 8f0410d9c31e5b33820925be685cb4528799c7da Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Thu, 13 Aug 2020 22:50:16 +0000 Subject: [PATCH 20/22] typo --- samples/error-correction/syndrome/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/error-correction/syndrome/README.md b/samples/error-correction/syndrome/README.md index a03155f19cf2..f9f4df1e453e 100644 --- a/samples/error-correction/syndrome/README.md +++ b/samples/error-correction/syndrome/README.md @@ -25,7 +25,7 @@ We start by preparing an auxiliary qubit into the superposition state by applyin ## Running the Sample -To run the sample, run `python syndrome.py --qubits `", where `` is replaced by the number of qubits. For more information, run `python syndrome.py --help`. +To run the sample, run `python syndrome.py --qubits `, where `` is replaced by the number of qubits. For more information, run `python syndrome.py --help`. ## Manifest From ca11e39ccaad75bd43fd79e0f65cc537b837b837 Mon Sep 17 00:00:00 2001 From: Guen P Date: Thu, 13 Aug 2020 17:42:46 -0700 Subject: [PATCH 21/22] Apply suggestions from code review README review edits Co-authored-by: Chris Granade --- samples/error-correction/syndrome/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/samples/error-correction/syndrome/README.md b/samples/error-correction/syndrome/README.md index f9f4df1e453e..fb647484e34a 100644 --- a/samples/error-correction/syndrome/README.md +++ b/samples/error-correction/syndrome/README.md @@ -2,22 +2,23 @@ page_type: sample languages: - qsharp +- python products: - qdk -description: "This sample uses the Q# standard libraries to implement a Syndrome for detecting errors in a given number of data qubits." -urlFragment: syndrome +description: "This sample uses the Q# standard libraries to implement a syndrome for detecting errors in a given number of data qubits." +urlFragment: quantum-syndrome --- -# Syndrome Sample +# Measuring quantum error syndromes with Q\# -This sample demonstrates how to create a partially implemented Syndrome for detecting errors generated in noisy hardware. The sample shows how this is implemented using Q# standard libraries. Currently, it does not simulate realistic noise so the resulting unitary operator is trivial, however, it is useful to demonstrate the concept of a Syndrome. +This sample demonstrates how to create a partially implemented Syndrome for detecting errors generated in noisy hardware. The sample shows how this is implemented using Q# standard libraries. Currently, it does not simulate realistic noise so the resulting unitary operator is trivial. However, the program is useful to demonstrate the concept of a syndrome. The algorithm used is described in [Surface codes: Towards practical large-scale quantum computation](https://arxiv.org/abs/1208.0928) by Fowler et al. In particular, this example implements the circuit shown in Figure 1 c. in this paper. The circuit uses _N data qubits_, where _N_ is a number given as input to the script, plus one _auxiliary_ qubit. The principle of the circuit is as follows. First, we prepare our data qubits in random states of a set of random bases, in order to simulate a noisy process. The circuit then propagates these errors to the auxiliary qubit using the principle of _phase kickback_. Phase kickback works by encoding pieces of a quantum algorithm into the global phase of an extra auxiliary qubit. -We start by preparing an auxiliary qubit into the superposition state by applying a _H_ gate, to change to the _X_ computational basis. Subsequently, we apply controlled Pauli operators to each of the data qubits, using the auxiliary qubit as control. The goal is to create a global phase shift on the auxiliary qubit, that will depend on the state of the basis qubits. After the circuit runs, the auxiliary qubit is measured in the _X_-basis, which reveals its phase information. +We start by preparing an auxiliary qubit into the superposition state by applying a `H` operation, to change to the _X_ computational basis. Subsequently, we apply controlled Pauli operators to each of the data qubits, using the auxiliary qubit as control. The goal is to create a global phase shift on the auxiliary qubit, that will depend on the state of the basis qubits. After the circuit runs, the auxiliary qubit is measured in the _X_-basis, which reveals its phase information. ## Prerequisites From be305e68ff3971a7f3d9ee1fdc5479f44550e14b Mon Sep 17 00:00:00 2001 From: Guen Prawiroatmodjo Date: Fri, 14 Aug 2020 01:10:42 +0000 Subject: [PATCH 22/22] fix bug: use random order to apply CNOT gates, update README to reflect that --- samples/error-correction/syndrome/README.md | 4 ++-- samples/error-correction/syndrome/Syndrome.qs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/error-correction/syndrome/README.md b/samples/error-correction/syndrome/README.md index fb647484e34a..fcb709c6b777 100644 --- a/samples/error-correction/syndrome/README.md +++ b/samples/error-correction/syndrome/README.md @@ -16,9 +16,9 @@ This sample demonstrates how to create a partially implemented Syndrome for dete The algorithm used is described in [Surface codes: Towards practical large-scale quantum computation](https://arxiv.org/abs/1208.0928) by Fowler et al. In particular, this example implements the circuit shown in Figure 1 c. in this paper. -The circuit uses _N data qubits_, where _N_ is a number given as input to the script, plus one _auxiliary_ qubit. The principle of the circuit is as follows. First, we prepare our data qubits in random states of a set of random bases, in order to simulate a noisy process. The circuit then propagates these errors to the auxiliary qubit using the principle of _phase kickback_. Phase kickback works by encoding pieces of a quantum algorithm into the global phase of an extra auxiliary qubit. +The circuit uses _N data qubits_, where _N_ is a number given as input to the script, plus one _auxiliary_ qubit. The principle of the circuit is as follows. First, we prepare our data qubits in random states of a set of random bases, in order to simulate a noisy process. The circuit then propagates these errors to the auxiliary qubit using the principle of _phase kickback_. Phase kickback works by encoding pieces of a quantum algorithm into the global phase of an extra (auxiliary) qubit. -We start by preparing an auxiliary qubit into the superposition state by applying a `H` operation, to change to the _X_ computational basis. Subsequently, we apply controlled Pauli operators to each of the data qubits, using the auxiliary qubit as control. The goal is to create a global phase shift on the auxiliary qubit, that will depend on the state of the basis qubits. After the circuit runs, the auxiliary qubit is measured in the _X_-basis, which reveals its phase information. +We start by preparing an auxiliary qubit into the superposition state by applying a `H` operation, to change to the _X_ computational basis. Subsequently, we apply controlled Pauli operators to each of the data qubits in random order, using the auxiliary qubit as control. The goal is to create a global phase shift on the auxiliary qubit, that will depend on the state of the data qubits. After the circuit runs, the auxiliary qubit is measured in the _X_-basis, which reveals its phase information. ## Prerequisites diff --git a/samples/error-correction/syndrome/Syndrome.qs b/samples/error-correction/syndrome/Syndrome.qs index 353f925464d1..1d80005989c2 100644 --- a/samples/error-correction/syndrome/Syndrome.qs +++ b/samples/error-correction/syndrome/Syndrome.qs @@ -89,8 +89,8 @@ namespace Microsoft.Quantum.Samples.ErrorCorrection.Syndrome { H(auxiliary); // Apply Controlled Pauli operations to data qubits, resulting in a phase kickback /// on the auxiliary qubit - for ((qubit, basis) in Zip(block, encodingBases)) { - Controlled ApplyPauli([auxiliary], ([basis], [qubit])); + for ((index, basis) in Zip(qubitIndices, encodingBases)) { + Controlled ApplyPauli([auxiliary], ([basis], [block[index]])); } let auxiliaryResult = Measure([PauliX], [auxiliary]); let dataResult = ForEach(BasisMeasure, Zip(encodingBases, block));