qsharp-lang 1.0.25-dev → 1.0.27-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -42,7 +42,7 @@ export default [
|
|
|
42
42
|
{
|
|
43
43
|
"title": "Deutsch-Jozsa",
|
|
44
44
|
"shots": 1,
|
|
45
|
-
"code": "/// # Sample\n/// Deutsch–Jozsa algorithm\n///\n/// # Description\n/// Deutsch–Jozsa is a quantum algorithm that determines whether a given Boolean\n/// function 𝑓 is constant (0 on all inputs or 1 on all inputs) or balanced\n/// (1 for exactly half of the input domain and 0 for the other half).\n///\n/// This Q# program implements the Deutsch–Jozsa algorithm.\nnamespace Sample {\n
|
|
45
|
+
"code": "/// # Sample\n/// Deutsch–Jozsa algorithm\n///\n/// # Description\n/// Deutsch–Jozsa is a quantum algorithm that determines whether a given Boolean\n/// function 𝑓 is constant (0 on all inputs or 1 on all inputs) or balanced\n/// (1 for exactly half of the input domain and 0 for the other half).\n///\n/// This Q# program implements the Deutsch–Jozsa algorithm.\nnamespace Sample {\n open Microsoft.Quantum.Measurement;\n\n @EntryPoint()\n operation Main() : (Result[], Result[]) {\n // A Boolean function is a function that maps bitstrings to a bit:\n // 𝑓 : {0, 1}^n → {0, 1}.\n\n // We say that 𝑓 is constant if 𝑓(𝑥⃗) = 𝑓(𝑦⃗) for all bitstrings 𝑥⃗ and\n // 𝑦⃗, and that 𝑓 is balanced if 𝑓 evaluates to true for exactly half of\n // its inputs.\n\n // If we are given a function 𝑓 as a quantum operation 𝑈 |𝑥〉|𝑦〉 =\n // |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉, and are promised that 𝑓 is either constant or is\n // balanced, then the Deutsch–Jozsa algorithm decides between these\n // cases with a single application of 𝑈.\n\n // Here, we demonstrate the use of the Deutsch-Jozsa algorithm by\n // determining the type (constant or balanced) of a couple of functions.\n let balancedResults = DeutschJozsa(SimpleBalancedBoolF, 5);\n let constantResults = DeutschJozsa(SimpleConstantBoolF, 5);\n return (balancedResults, constantResults);\n }\n\n /// # Summary\n /// This operation implements the DeutschJozsa algorithm.\n /// It returns the query register measurement results. If all the measurement\n /// results are `Zero`, the function is constant. If at least one measurement\n /// result is `One`, the function is balanced.\n /// It is assumed that the function is either constant or balanced.\n ///\n /// # Input\n /// ## Uf\n /// A quantum operation that implements |𝑥〉|𝑦〉 ↦ |𝑥〉|𝑦 ⊕ 𝑓(𝑥)〉, where 𝑓 is a\n /// Boolean function, 𝑥 is an 𝑛 bit register and 𝑦 is a single qubit.\n /// ## n\n /// The number of bits in the input register |𝑥〉.\n ///\n /// # Output\n /// An array of measurement results for the query reguster.\n /// All `Zero` measurement results indicate that the function is constant.\n /// At least one `One` measurement result in the array indicates that the\n /// function is balanced.\n ///\n /// # See Also\n /// - For details see Section 1.4.3 of Nielsen & Chuang.\n ///\n /// # References\n /// - [ *Michael A. Nielsen , Isaac L. Chuang*,\n /// Quantum Computation and Quantum Information ]\n /// (http://doi.org/10.1017/CBO9780511976667)\n operation DeutschJozsa(Uf : ((Qubit[], Qubit) => Unit), n : Int) : Result[] {\n // We allocate n + 1 clean qubits. Note that the function `Uf` is defined\n // on inputs of the form (x, y), where x has n bits and y has 1 bit.\n use queryRegister = Qubit[n];\n use target = Qubit();\n\n // The last qubit needs to be flipped so that the function will actually\n // be computed into the phase when Uf is applied.\n X(target);\n\n // Now, a Hadamard transform is applied to each of the qubits.\n H(target);\n // We use a within-apply block to ensure that the Hadamard transform is\n // correctly inverted on the |𝑥〉 register.\n within {\n for q in queryRegister {\n H(q);\n }\n } apply {\n // We apply Uf to the n+1 qubits, computing |𝑥, 𝑦〉 ↦ |𝑥, 𝑦 ⊕ 𝑓(𝑥)〉.\n Uf(queryRegister, target);\n }\n\n // Measure the query register and reset all qubits so they can be safely\n // deallocated.\n let results = MeasureEachZ(queryRegister);\n ResetAll(queryRegister);\n Reset(target);\n return results;\n }\n\n // Simple constant Boolean function\n operation SimpleConstantBoolF(args : Qubit[], target : Qubit) : Unit {\n X(target);\n }\n\n // Simple balanced Boolean function\n operation SimpleBalancedBoolF(args : Qubit[], target : Qubit) : Unit {\n CX(args[0], target);\n }\n}\n\n"
|
|
46
46
|
},
|
|
47
47
|
{
|
|
48
48
|
"title": "Deutsch-Jozsa (Advanced)",
|
|
@@ -62,7 +62,7 @@ export default [
|
|
|
62
62
|
{
|
|
63
63
|
"title": "Grover's search",
|
|
64
64
|
"shots": 100,
|
|
65
|
-
"code": "/// # Sample\n/// Grover's search algorithm\n///\n/// # Description\n/// Grover's search algorithm is a quantum algorithm that finds with high\n/// probability the unique input to a black box function that produces a\n/// particular output value.\n///\n/// This Q# program implements the Grover's search algorithm.\nnamespace Sample {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Measurement;\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation Main() : Result[] {\n let nQubits = 5;\n\n // Grover's algorithm relies on performing a \"Grover iteration\" an\n // optimal number of times to maximize the probability of finding the\n // value we are searching for.\n // You can set the number iterations to a value lower than optimal to\n // intentionally reduce precision.\n let iterations = CalculateOptimalIterations(nQubits);\n Message($\"Number of iterations: {iterations}\");\n\n // Use Grover's algorithm to find a particular marked state.\n let results = GroverSearch(nQubits, iterations, ReflectAboutMarked);\n return results;\n }\n\n /// # Summary\n /// Implements Grover's algorithm, which searches all possible inputs to an\n /// operation to find a particular marked state.\n operation GroverSearch(\n nQubits : Int,\n iterations : Int,\n phaseOracle : Qubit[] => Unit) : Result[] {\n\n use qubits = Qubit[nQubits];\n\n // Initialize a uniform superposition over all possible inputs.\n PrepareUniform(qubits);\n\n // The search itself consists of repeatedly reflecting about the marked\n // state and our start state, which we can write out in Q# as a for loop.\n for _ in 1..iterations {\n phaseOracle(qubits);\n ReflectAboutUniform(qubits);\n }\n\n // Measure and return the answer.\n return MResetEachZ(qubits);\n }\n\n /// # Summary\n /// Returns the optimal number of Grover iterations needed to find a marked\n /// item, given the number of qubits in a register.\n function CalculateOptimalIterations(nQubits : Int) : Int {\n let nItems = 1 <<< nQubits; // 2^nQubits\n let angle = ArcSin(1. / Sqrt(IntAsDouble(nItems)));\n let iterations = Round(0.25 * PI() / angle - 0.5);\n return iterations;\n }\n\n /// # Summary\n /// Reflects about the basis state marked by alternating zeros and ones.\n /// This operation defines what input we are trying to find in the search.\n operation ReflectAboutMarked(inputQubits : Qubit[]) : Unit {\n Message(\"Reflecting about marked state...\");\n use outputQubit = Qubit();\n within {\n // We initialize the outputQubit to (|0⟩ - |1⟩) / √2, so that\n // toggling it results in a (-1) phase.\n X(outputQubit);\n H(outputQubit);\n // Flip the outputQubit for marked states.\n // Here, we get the state with alternating 0s and 1s by using the X\n // operation on every other qubit.\n for q in inputQubits[...2...] {\n X(q);\n }\n } apply {\n Controlled X(inputQubits, outputQubit);\n }\n }\n\n /// # Summary\n /// Given a register in the all-zeros state, prepares a uniform\n /// superposition over all basis states.\n operation PrepareUniform(inputQubits : Qubit[]) : Unit is Adj + Ctl {\n for q in inputQubits {\n H(q);\n }\n }\n\n /// # Summary\n /// Reflects about the all-ones state.\n operation ReflectAboutAllOnes(inputQubits : Qubit[]) : Unit {\n Controlled Z(Most(inputQubits), Tail(inputQubits));\n }\n\n /// # Summary\n /// Reflects about the uniform superposition state.\n operation ReflectAboutUniform(inputQubits : Qubit[]) : Unit {\n within {\n // Transform the uniform superposition to all-zero.\n Adjoint PrepareUniform(inputQubits);\n // Transform the all-zero state to all-ones\n for q in inputQubits {\n X(q);\n }\n } apply {\n // Now that we've transformed the uniform superposition to the\n // all-ones state, reflect about the all-ones state, then let the\n // within/apply block transform us back.\n ReflectAboutAllOnes(inputQubits);\n }\n }\n}\n"
|
|
65
|
+
"code": "/// # Sample\n/// Grover's search algorithm\n///\n/// # Description\n/// Grover's search algorithm is a quantum algorithm that finds with high\n/// probability the unique input to a black box function that produces a\n/// particular output value.\n///\n/// This Q# program implements the Grover's search algorithm.\nnamespace Sample {\n open Microsoft.Quantum.Convert;\n open Microsoft.Quantum.Math;\n open Microsoft.Quantum.Arrays;\n open Microsoft.Quantum.Measurement;\n open Microsoft.Quantum.Diagnostics;\n\n @EntryPoint()\n operation Main() : Result[] {\n let nQubits = 5;\n\n // Grover's algorithm relies on performing a \"Grover iteration\" an\n // optimal number of times to maximize the probability of finding the\n // value we are searching for.\n // You can set the number iterations to a value lower than optimal to\n // intentionally reduce precision.\n let iterations = CalculateOptimalIterations(nQubits);\n Message($\"Number of iterations: {iterations}\");\n\n // Use Grover's algorithm to find a particular marked state.\n let results = GroverSearch(nQubits, iterations, ReflectAboutMarked);\n return results;\n }\n\n /// # Summary\n /// Implements Grover's algorithm, which searches all possible inputs to an\n /// operation to find a particular marked state.\n operation GroverSearch(\n nQubits : Int,\n iterations : Int,\n phaseOracle : Qubit[] => Unit) : Result[] {\n\n use qubits = Qubit[nQubits];\n\n // Initialize a uniform superposition over all possible inputs.\n PrepareUniform(qubits);\n\n // The search itself consists of repeatedly reflecting about the marked\n // state and our start state, which we can write out in Q# as a for loop.\n for _ in 1..iterations {\n phaseOracle(qubits);\n ReflectAboutUniform(qubits);\n }\n\n // Measure and return the answer.\n return MResetEachZ(qubits);\n }\n\n /// # Summary\n /// Returns the optimal number of Grover iterations needed to find a marked\n /// item, given the number of qubits in a register.\n function CalculateOptimalIterations(nQubits : Int) : Int {\n if nQubits > 63 {\n fail \"This sample supports at most 63 qubits.\";\n }\n let nItems = 1 <<< nQubits; // 2^nQubits\n let angle = ArcSin(1. / Sqrt(IntAsDouble(nItems)));\n let iterations = Round(0.25 * PI() / angle - 0.5);\n return iterations;\n }\n\n /// # Summary\n /// Reflects about the basis state marked by alternating zeros and ones.\n /// This operation defines what input we are trying to find in the search.\n operation ReflectAboutMarked(inputQubits : Qubit[]) : Unit {\n Message(\"Reflecting about marked state...\");\n use outputQubit = Qubit();\n within {\n // We initialize the outputQubit to (|0⟩ - |1⟩) / √2, so that\n // toggling it results in a (-1) phase.\n X(outputQubit);\n H(outputQubit);\n // Flip the outputQubit for marked states.\n // Here, we get the state with alternating 0s and 1s by using the X\n // operation on every other qubit.\n for q in inputQubits[...2...] {\n X(q);\n }\n } apply {\n Controlled X(inputQubits, outputQubit);\n }\n }\n\n /// # Summary\n /// Given a register in the all-zeros state, prepares a uniform\n /// superposition over all basis states.\n operation PrepareUniform(inputQubits : Qubit[]) : Unit is Adj + Ctl {\n for q in inputQubits {\n H(q);\n }\n }\n\n /// # Summary\n /// Reflects about the all-ones state.\n operation ReflectAboutAllOnes(inputQubits : Qubit[]) : Unit {\n Controlled Z(Most(inputQubits), Tail(inputQubits));\n }\n\n /// # Summary\n /// Reflects about the uniform superposition state.\n operation ReflectAboutUniform(inputQubits : Qubit[]) : Unit {\n within {\n // Transform the uniform superposition to all-zero.\n Adjoint PrepareUniform(inputQubits);\n // Transform the all-zero state to all-ones\n for q in inputQubits {\n X(q);\n }\n } apply {\n // Now that we've transformed the uniform superposition to the\n // all-ones state, reflect about the all-ones state, then let the\n // within/apply block transform us back.\n ReflectAboutAllOnes(inputQubits);\n }\n }\n}\n"
|
|
66
66
|
},
|
|
67
67
|
{
|
|
68
68
|
"title": "Hidden Shift",
|
|
Binary file
|
package/lib/web/qsc_wasm_bg.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/ux/qsharp-ux.css
CHANGED
|
@@ -35,7 +35,7 @@ modern-normalize (see https://mattbrictson.com/blog/css-normalize-and-reset for
|
|
|
35
35
|
);
|
|
36
36
|
--qs-tr-nth-color: var(
|
|
37
37
|
--vscode-list-hoverBackground,
|
|
38
|
-
var(--jp-layout-color1, #f2f2f2)
|
|
38
|
+
var(--jp-layout-color1, var(--colab-secondary-surface-color, #f2f2f2))
|
|
39
39
|
);
|
|
40
40
|
--nav-background: #bed1f4;
|
|
41
41
|
--nav-hover-background: #b3bede;
|
|
@@ -325,6 +325,7 @@ html {
|
|
|
325
325
|
|
|
326
326
|
.histogram {
|
|
327
327
|
max-height: calc(100vh - 40px);
|
|
328
|
+
max-width: 600px;
|
|
328
329
|
border: 1px solid var(--border-color);
|
|
329
330
|
background-color: var(--vscode-sideBar-background, white);
|
|
330
331
|
}
|
package/ux/spaceChart.tsx
CHANGED
|
@@ -60,7 +60,7 @@ export function SpaceChart(props: { estimatesData: ReData }) {
|
|
|
60
60
|
>
|
|
61
61
|
<path
|
|
62
62
|
d={getPieSegment(250, 185, 180, 0, breakAngle, 120)}
|
|
63
|
-
fill="var(--vscode-charts-
|
|
63
|
+
fill="var(--vscode-charts-orange, orange)"
|
|
64
64
|
stroke="white"
|
|
65
65
|
></path>
|
|
66
66
|
<path
|
|
@@ -79,7 +79,7 @@ export function SpaceChart(props: { estimatesData: ReData }) {
|
|
|
79
79
|
y="400"
|
|
80
80
|
width="25"
|
|
81
81
|
height="25"
|
|
82
|
-
fill="var(--vscode-charts-
|
|
82
|
+
fill="var(--vscode-charts-orange, orange)"
|
|
83
83
|
stroke="white"
|
|
84
84
|
stroke-width="1"
|
|
85
85
|
/>
|