{"id":19115064,"url":"https://github.com/ptahmose/qsharp-playground","last_synced_at":"2026-06-18T02:31:53.283Z","repository":{"id":71234054,"uuid":"116186434","full_name":"ptahmose/QSharp-Playground","owner":"ptahmose","description":"Playing around with Q#","archived":false,"fork":false,"pushed_at":"2024-01-15T23:41:10.000Z","size":12021,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-22T12:13:49.685Z","etag":null,"topics":["grovers-algorithm","qs","qsharp","quantum-computing"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ptahmose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-01-03T22:04:22.000Z","updated_at":"2024-01-15T22:44:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"546c3283-0e30-4ebc-9ae2-29276b4fc1b2","html_url":"https://github.com/ptahmose/QSharp-Playground","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ptahmose/QSharp-Playground","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptahmose%2FQSharp-Playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptahmose%2FQSharp-Playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptahmose%2FQSharp-Playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptahmose%2FQSharp-Playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ptahmose","download_url":"https://codeload.github.com/ptahmose/QSharp-Playground/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptahmose%2FQSharp-Playground/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34474063,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-18T02:00:06.871Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grovers-algorithm","qs","qsharp","quantum-computing"],"created_at":"2024-11-09T04:45:25.987Z","updated_at":"2026-06-18T02:31:53.259Z","avatar_url":"https://github.com/ptahmose.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# QSharp-Playground\nPlaying around with Q#\n\nTrying to learn some of the basic concepts of quantum computing with [Q#](https://docs.microsoft.com/en-us/quantum/quantum-qr-intro?view=qsharp-preview). Q# is Microsoft's domain-specific programming language used for expressing quantum algorithms. It is part of the [Microsoft Quantum Development Kit](https://www.microsoft.com/en-us/quantum/development-kit) - which is an [early preview version](https://blogs.microsoft.com/ai/future-quantum-microsoft-releases-free-preview-quantum-development-kit/).\n\nYou will find in this repository:\n- an implementation of a quantum 4-bit adder\n- a oracle function using this 4-bit adder\n- leverage this oracle function by applying Grover's algorithm in order to search for two summands which give a specified sum\n\n\n## 4-qubit adder\n\nThe implementation of the 4-bit adder follows the approach detailed [here](https://arxiv.org/ftp/quant-ph/papers/0206/0206028.pdf). This *Conventional Quantum Plain Adder* takes two 4-qubit inputs and produces a 5-qubit output. In addition, we need to have 4 ancillary qubits.\n\nThe prototype of the adder is:\n\n    operation FourBitAdder(a1: Qubit,a2: Qubit,a3: Qubit,a4: Qubit,b1: Qubit,b2: Qubit,b3: Qubit,b4: Qubit,carry:Qubit, tempQubits:Qubit[]):()\n\nNotes:\n- The result is placed in the registers b1, b2, b3, b4, carry.\n- Quantum operations have to be reversible, which this gate ensures.\n- The 4 ancillary registers (tempQubits) have to be in pure state |0\u003e on input, and they are |0\u003e on output.\n- The carry-qubit has to be in state |0\u003e on input.\n\n## 4-qubit adder in action\n\nThe command-line program \"4BitAdderAndGrover.exe\" allows to call this quantum 4-bit-adder with the inputs in a mixed state, and in a sense we calculate the sum for all possible inputs **at once** - and if we do a measurement on the result, we get **one** of those possibilities.\n\nHere is an example:\n\n    \u003e.\\4BitAdderAndGrover.exe -o TestAdderWithEntangledInput -r 10\n    Executing the 4-bit adder with entangled inputs\n\n    20 = 7 + 13\n    19 = 11 + 8\n    26 = 14 + 12\n    5 = 1 + 4\n    23 = 12 + 11\n    11 = 4 + 7\n    11 = 5 + 6\n    8 = 7 + 1\n    13 = 11 + 2\n    16 = 5 + 11\n    *** Everything OK. ***\n\nOf course - this is quite useless. It is mind-blowing that the quantum computer is able to do the computation for all possible inputs **at once** in **one step**, but what's the use if we are only able to get **one** of those results - and we are not even able to control which one. Or... can we?\n\n## Grover's Algorithm\n\nIt seems to turn out, that we actually **can control** (to some extent) which of the results we get when measuring. That's what [Grover's Search Algorithm](https://en.wikipedia.org/wiki/Grover%27s_algorithm) is about (I found this [paper](https://arxiv.org/abs/quant-ph/9809016v2) very instructive).\nThe ingredient to Grover's algorithm is an *oracle function* which has the task of\n* bring the input/output qubits in superposition\n* determine whether the input qubits give the result we are looking for\n* if so, set the flag-qubit to |1\u003e\n\nThis allows us to specify a result, and then determine the input which gives the result. So, we can solve the question \"for which input do I get this result?\".\n\nWe need to repeat Grover's algorithm $\\frac{\\pi}{4}\\cdot\\sqrt{\\frac{2^n}{k}}$ times in order to get maximum likelihood of measuring the desired result (where the flag-qubit is |1\u003e) - where n is the number of input qubits and k is the multiplicity of \"good inputs\". The latter is unknown in general - which then goes beyond the basic application of Grover.\n\nThis example determines which summands give a sum of 29 (and repeat this calculation 3 times):\n\n    \u003e .\\4BitAdderAndGrover.exe --operation FindSummands -e 29 -g 9 -r 3\n    Finding the summands which give the specified sum (29), using 9 Grover-iterations.\n    \n    1: a=15 b=14  ; 751 0000'1'0'1110'1111\n    2: a=15 b=14  ; 751 0000'1'0'1110'1111\n    3: a=14 b=15  ; 766 0000'1'0'1111'1110\n    Grover-Iterations 9: 3 of 3 had the desired result.\n\nWe had to iterate 9 times because $\\frac{\\pi}{4}\\cdot\\sqrt{\\frac{2^8}{2}}$ is approximately 9 (and there are two possible sets of summands). If we use an inappropriate number of iterations, we are more likely to measure a \"bad state\":\n\n    \u003e .\\4BitAdderAndGrover.exe --operation FindSummands -e 29 -g 3 -r 5\n    Finding the summands which give the specified sum (29), using 3 Grover-iterations.\n    \n    1: a=15 b=14  ; 751 0000'1'0'1110'1111\n    2: not successful\n    3: a=14 b=15  ; 766 0000'1'0'1111'1110\n    4: a=15 b=14  ; 751 0000'1'0'1110'1111\n    5: not successful\n    Grover-Iterations 3: 3 of 5 had the desired result.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fptahmose%2Fqsharp-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fptahmose%2Fqsharp-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fptahmose%2Fqsharp-playground/lists"}