Skip to content

Instantly share code, notes, and snippets.

@crazy4pi314
Created August 11, 2020 16:22
Show Gist options
  • Save crazy4pi314/b2b2fe3de5cd14ce9f683ffbdd862d5c to your computer and use it in GitHub Desktop.
Save crazy4pi314/b2b2fe3de5cd14ce9f683ffbdd862d5c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/json": "{\"rows\":[{\"@type\":\"@tuple\",\"Item1\":\"iqsharp\",\"Item2\":\"0.12.20080423-beta\"},{\"@type\":\"@tuple\",\"Item1\":\"Jupyter Core\",\"Item2\":\"1.4.0.0\"},{\"@type\":\"@tuple\",\"Item1\":\".NET Runtime\",\"Item2\":\".NETCoreApp,Version=v3.1\"}]}",
"text/html": [
"<table><thead><tr><th style=\"text-align: start;\">Component</th><th style=\"text-align: start;\">Version</th></tr></thead><tbody><tr><td style=\"text-align: start;\">iqsharp</td><td style=\"text-align: start;\">0.12.20080423-beta</td></tr><tr><td style=\"text-align: start;\">Jupyter Core</td><td style=\"text-align: start;\">1.4.0.0</td></tr><tr><td style=\"text-align: start;\">.NET Runtime</td><td style=\"text-align: start;\">.NETCoreApp,Version=v3.1</td></tr></tbody></table>"
],
"text/plain": [
"Component Version\r\n",
"------------ ------------------------\r\n",
"iqsharp 0.12.20080423-beta\r\n",
"Jupyter Core 1.4.0.0\r\n",
".NET Runtime .NETCoreApp,Version=v3.1\r\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%version"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/json": "[\"Microsoft.Quantum.Samples.SimpleGrover.NIterations\",\"Microsoft.Quantum.Samples.SimpleGrover.PrepareAllOnes\",\"Microsoft.Quantum.Samples.SimpleGrover.PrepareUniform\",\"Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutAllOnes\",\"Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutMarked\",\"Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutUniform\",\"Microsoft.Quantum.Samples.SimpleGrover.SearchForMarkedInput\"]",
"text/html": [
"<ul><li>Microsoft.Quantum.Samples.SimpleGrover.NIterations</li><li>Microsoft.Quantum.Samples.SimpleGrover.PrepareAllOnes</li><li>Microsoft.Quantum.Samples.SimpleGrover.PrepareUniform</li><li>Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutAllOnes</li><li>Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutMarked</li><li>Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutUniform</li><li>Microsoft.Quantum.Samples.SimpleGrover.SearchForMarkedInput</li></ul>"
],
"text/plain": [
"Microsoft.Quantum.Samples.SimpleGrover.NIterations, Microsoft.Quantum.Samples.SimpleGrover.PrepareAllOnes, Microsoft.Quantum.Samples.SimpleGrover.PrepareUniform, Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutAllOnes, Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutMarked, Microsoft.Quantum.Samples.SimpleGrover.ReflectAboutUniform, Microsoft.Quantum.Samples.SimpleGrover.SearchForMarkedInput"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%workspace reload"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reflecting about marked state...\r\n"
]
},
{
"data": {
"application/json": "[0]",
"text/html": [
"<ul><li>Zero</li></ul>"
],
"text/plain": [
"Zero"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%simulate Microsoft.Quantum.Samples.SimpleGrover.SearchForMarkedInput nQubits=1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/json": "{\"Value\":\"\\r\\n <div id=\\\"416c8154-b60e-47b7-871c-2c4b005d1f96\\\"></div>\\r\\n \"}",
"text/html": [
"\r\n",
" <div id=\"416c8154-b60e-47b7-871c-2c4b005d1f96\"></div>\r\n",
" "
],
"text/plain": [
"Microsoft.Quantum.IQSharp.Kernel.RawHtmlPayload"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%debug Microsoft.Quantum.Samples.SimpleGrover.SearchForMarkedInput nQubits=1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Q#",
"language": "qsharp",
"name": "iqsharp"
},
"language_info": {
"file_extension": ".qs",
"mimetype": "text/x-qsharp",
"name": "qsharp",
"version": "0.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.SimpleGrover {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
/// # Summary
/// This operation applies Grover's algorithm to search all possible inputs
/// to an operation to find a particular marked state.
operation SearchForMarkedInput(nQubits : Int) : Result[] {
using (qubits = Qubit[nQubits]) {
// Initialize a uniform superposition over all possible inputs.
PrepareUniform(qubits);
// The search itself consists of repeatedly reflecting about the
// marked state and our start state, which we can write out in Q#
// as a for loop.
for (idxIteration in 0..NIterations(nQubits) - 1) {
ReflectAboutMarked(qubits);
ReflectAboutUniform(qubits);
}
// Measure and return the answer.
return ForEach(MResetZ, qubits);
}
}
/// # Summary
/// Returns the number of Grover iterations needed to find a single marked
/// item, given the number of qubits in a register.
function NIterations(nQubits : Int) : Int {
let nItems = 1 <<< nQubits; // 2^numQubits
// compute number of iterations:
let angle = ArcSin(1. / Sqrt(IntAsDouble(nItems)));
let nIterations = Round(0.25 * PI() / angle - 0.5);
return nIterations;
}
/// # Summary
/// Reflects about the basis state marked by alternating zeros and ones.
/// This operation defines what input we are trying to find in the main
/// search.
operation ReflectAboutMarked(inputQubits : Qubit[]) : Unit {
Message("Reflecting about marked state...");
using (outputQubit = Qubit()) {
within {
// We initialize the outputQubit to (|0⟩ - |1⟩) / √2,
// so that toggling it results in a (-1) phase.
X(outputQubit);
H(outputQubit);
// Flip the outputQubit for marked states.
// Here, we get the state with alternating 0s and 1s by using
// the X instruction on every other qubit.
ApplyToEachA(X, inputQubits[...2...]);
} apply {
Controlled X(inputQubits, outputQubit);
}
}
}
/// # Summary
/// Reflects about the uniform superposition state.
operation ReflectAboutUniform(inputQubits : Qubit[]) : Unit {
within {
// Transform the uniform superposition to all-zero.
Adjoint PrepareUniform(inputQubits);
// Transform the all-zero state to all-ones
PrepareAllOnes(inputQubits);
} apply {
// Now that we've transformed the uniform superposition to the
// all-ones state, reflect about the all-ones state, then let
// the within/apply block transform us back.
ReflectAboutAllOnes(inputQubits);
}
}
/// # Summary
/// Reflects about the all-ones state.
operation ReflectAboutAllOnes(inputQubits : Qubit[]) : Unit {
Controlled Z(Most(inputQubits), Tail(inputQubits));
}
/// # Summary
/// Given a register in the all-zeros state, prepares a uniform
/// superposition over all basis states.
operation PrepareUniform(inputQubits : Qubit[]) : Unit is Adj + Ctl {
ApplyToEachCA(H, inputQubits);
}
/// # Summary
/// Given a register in the all-zeros state, prepares an all-ones state
/// by flipping every qubit.
operation PrepareAllOnes(inputQubits : Qubit[]) : Unit is Adj + Ctl {
ApplyToEachCA(X, inputQubits);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment