Q# – Introduction to quantum computing

, 14 minutes to read

Mi­crosoft has re­leased a new .NET lan­guage called Q# along with Mi­crosoft Quan­tum De­vel­op­ment Kit. It con­tains a full state quan­tum sim­u­la­tor. Mi­crosoft want to pave the path to quan­tum com­put­ing in dat­a­cen­ters able to ex­e­cute pow­er­ful quan­tum al­gorithms. A quan­tum com­puter is fun­da­men­tally dif­fer­ent than a clas­si­cal bi­nary com­puter. A new era of com­put­ing may be closer than you may think.

What you need

How to install and run

What it simulates

Clas­si­cal com­put­ers are based on bi­nary num­bers. The bit can be re­al­ized by an elec­tric charge in the ca­pac­i­tor or by a mag­ne­tized area in the fer­ro­mag­netic ma­te­rial. Spin-based quan­tum com­puters are based on qu­bits. The qu­bit can be re­al­ized as a phys­i­cal par­ti­cle spin. They were pro­posed by Richard Feyn­man on Con­fer­ence on Physics and Com­pu­ta­tion at MIT in 1981.

What is the spin? What else can also be con­sidered as a qu­bit? The qubit can be rep­re­sented by Earth’s ro­ta­tion. We know that the Earth ax­ial tilt (an an­gle be­tween equ­a­tor and or­bit) is 23°. The qubit is a unary vec­tor. We can read only one com­po­nent of the vec­tor. It im­plies that the qu­bit can­not be clone­able. With a qu­bit, we can­not do the same thing as mem­cpy func­tion in C++.

We can de­fine the qu­bit more pre­cisely as a lin­ear com­bi­na­tion of two vec­tors: |ψ⟩ = α ∙ |0⟩ + β ∙|1⟩, where α, β ∈ ℂ.

The qu­bit value can be read, but this pro­cess will po­lar­ize qu­bit to state of |0⟩ or |1⟩ with prob­a­bil­ity dis­tri­bu­tion re­lated to whichever state is near­est. This means that quan­tum com­put­ers are not uni­ver­sally faster than bit com­put­ers. Quan­tum com­put­ers are faster only for a small group of al­gorithms.

Quan­tum com­put­ers had lit­tle of in­ter­est un­til 1994. In that year Pe­ter Shor pub­lished an al­gorithm for quan­tum com­puter for in­te­ger fac­tor­iza­tion. It can be used to break the RSA al­gorithm. It re­quires 4098 qu­bits and 5.2 tril­lion Tof­foli gates to com­pute a 2048-bit RSA key. The time com­plex­ity of break­ing RSA with n-bit key on quan­tum com­puter is O(n³).

Quan­tum com­puter is ex­po­nen­tially faster than bit com­puter in dis­crete Fourier trans­form. It is very good in sim­u­lat­ing phys­i­cal sys­tems. It can be widely use­ful in ma­te­rial sci­ence, chem­istry and quan­tum chro­mo­dy­nam­ics.


Myth 1

The quan­tum com­puter is faster be­cause it can rep­re­sent an ex­po­nen­tial num­ber of states.

The binary com­puter can also dou­ble a num­ber of states when its mem­ory is ex­tended by a sin­gle bit. The num­ber of states grows ex­po­nen­tially with amount of mem­ory for both bi­nary com­puter and quan­tum com­puter.

Myth 2

The quan­tum com­puter is faster be­cause the qu­bit can rep­re­sent 0 and 1 at the same time.

Two bits can also rep­re­sent 0 and 1 at the same time. Does it mean that quan­tum com­puter is faster be­cause it needs half of mem­ory and quar­ter of tran­sis­tors than bi­nary com­put­ers? The ana­log com­puter can also rep­re­sent a su­per­po­si­tion 0 and 1 at the same time. Does it mean that ana­log com­put­ers are faster?

Myth 3

The quan­tum com­puter al­lows to eval­u­ate all pos­si­ble so­lu­tions in par­al­lel.

This is not true in gen­eral. Even the bi­nary com­puter can eval­u­ate all pos­si­ble so­lu­tions in par­al­lel when the prob­lem is sim­ple enough. Quan­tum com­puter is also lim­ited by amount of mem­ory and by num­ber of gates.

Myth 4

A qubit can hold an in­fi­nite num­ber of states.

Even a glass of wa­ter can hold an in­fi­nite num­ber of states but the prob­lem we have is we can­not mea­sure amount of wa­ter pre­cisely. Our mea­sure­ment al­ways has some de­vi­a­tion. It fixes us to a fi­nite (and quite small) num­ber of states. If a glass of wa­ter can be mea­sured as a qu­bit we would know that the glass is empty or full.

Myth 5

The quan­tum com­puter is faster be­cause it can eval­u­ate states of all qu­bits at once.

Cur­rently, 3-qu­bit gate is a max­i­mum. You can com­bine gates and put more of them in se­rial. Both bi­nary and quan­tum com­put­ers are lim­ited by a clock fre­quency (or by a speed at which the volt­age trav­els in the lead). By the way, you can con­fig­ure FPGA cir­cuit to eval­u­ate some op­er­a­tion for whole mem­ory. You can start with 8-bit mem­ory and 8-bit adder. Po­ten­tially you can end with GPU – which can per­form op­er­a­tions ex­po­nen­tially faster than CPU.

Myth 6

D-Wave is the first quan­tum com­puter.

D-Ware is a quan­tum an­nealer, which is far away from uni­ver­sal gate quan­tum com­puter. It can ex­e­cute only adi­a­batic quan­tum al­gorithms. While the D-Wave an­nealer is some­times 10 times faster than the bi­nary com­puter, it is also some­times more than 100 times slower.

Myth 7

En­tan­gled par­ti­cles al­low faster than speed of light com­mu­ni­ca­tion.

This is not true be­cause the mea­sure­ment is a de­struc­tive pro­cess.

What is good to know


The su­per­po­si­tion is a name of phys­i­cal prin­ci­ple. When one per­son speaks in a quiet en­vi­ron­ment you hear him. When many peo­ple gathered in a small place are speak­ing very close to each other you hear noth­ing but noise. But when those peo­ple would start say­ing the same thing at the same time as a choir, you will hear the speech much louder than in case of a sin­gle per­son. Two speakers are louder than one. This prin­ci­ple is called su­per­po­si­tion. You can ob­serve the same ef­fects with light­bulbs. Two light­bulbs are brighter than one. The eight-cylin­der en­gine has greater torque than a four-cylin­der. But the prin­ci­ple can­not be applied to ev­ery­thing. Two CPU cores aren’t two times faster than one core (in gen­eral). Sys­tems where su­per­po­si­tion works are called lin­ear.


Two ob­jects can be re­lated to each other. For ex­am­ple, a bike has one gear in the front and the sec­ond gear in the back con­nected by a chain. When one gear ro­tates, the sec­ond ro­tates as well be­cause the chain trans­mits the force. The con­nec­tion does not have to be nec­es­sary vis­i­ble – like a grav­ity. An ap­ple on the tree is con­nected to the Earth. When the stem snaps the ap­ple will move to­wards the Earth and the Earth will move to­wards the ap­ple un­til they hit each other.

Clas­si­cal me­chanic de­scribes a light as a stream of pho­tons. When a pho­ton passes through two (beta bar­ium bo­rate) crys­tals (that send dif­fer­ent po­lar­iza­tion states on dif­fer­ent tra­jec­to­ries), it spl­its into two pho­tons which are in­ter­con­nected. When you change the spin of the first, it ef­fects the spin of the sec­ond. This phe­nomenon is called the quan­tum en­tan­gle­ment. In fact, we shouldn’t imag­ine two en­tan­gled par­ti­cles as two sep­arate ob­jects, but as a sin­gle ob­ject which ex­ists on two lo­ca­tions at the same time be­cause the state be­tween them is trans­mit­ted in­s­tantly. How­ever, we can­not use that for com­mu­ni­ca­tion. Two mu­tu­ally en­tan­gled par­ti­cles may be called dif­fer­ently (don’t ask me why). They are usu­ally re­ferred as

Qu­bits are phys­i­cally re­al­ized as elec­tron spins. A cooper pair of elec­trons are nat­u­rally formed in a su­per­con­duc­tor. When it passes through two sep­arate crys­tals (quan­tum dots), it be­comes sep­arated. One elec­tron moves to the one lead and the other mem­ber of the pair to the sec­ond lead. En­tan­gled par­ti­cles in elec­tronic cir­cuits are gen­er­ated by this prin­ci­ple. Mi­crosoft has spent 12 years work­ing on its own qu­bit tech­nol­ogy based on Ma­jo­rana fermions.

Quantum Teleportation

Quan­tum tele­por­ta­tion is a term for trans­fer­ring a state of one qu­bit to an­other, without hav­ing to move a phys­i­cal par­ti­cle along with it.

We want to trans­fer a state of qu­bit ψ, which holds Al­ice, to Bob. To do so, we take two en­tan­gled qu­bits, α and β, and send α to Al­ice and β to Bob. Then Al­ice mea­sures a su­per­po­si­tion of ψ and α, so α′ = ψ + α. This mea­sure­ment also af­fects β which will change to β’ in a way that β′ = β + ψ. Then Al­ice sends α′ to Bob through a clas­si­cal com­mu­ni­ca­tion chan­nel. Bob will mod­ify β′ to β′′ in a way that β′′ = β′ + α′.

We now sup­pose that β′′ = ψ. Let’s ex­pand what Bob has. β′′ = β′ + α′ = β + ψ + ψ + α = ψ. The last equ­a­tion needs an ex­pla­na­tion. Two par­ti­cles α and β are en­tan­gled which also means that α + β = 0. A su­per­po­si­tion of two iden­ti­cal vec­tors is iden­ti­cal to orig­i­nal vec­tors be­cause all vec­tors are unit vec­tors, there­fore ψ + ψ = ψ.

If we still need to trans­fer α’ through an old-school com­mu­ni­ca­tion chan­nel, what’s the point of the quan­tum tele­por­ta­tion? In the be­gin­n­ing, the state of α is ran­dom. There­fore α′ doesn’t dis­close any use­ful in­for­ma­tion about ψ. This pro­to­col can be used in the quan­tum cryp­tog­ra­phy to trans­fer one-time pad. If the source of en­tan­gled qu­bits (in this case pho­tons) will be lo­cated on the satel­lite which will dis­tribute them to two re­mote lo­ca­tions, we can estab­lish a man-in-the-mid­dle-re­sis­tant com­mu­ni­ca­tion chan­nel.


Q# lan­guage is a new .NET lan­guage in­spired by C# and F#. If you learned those two lan­guages, you will learn Q# very eas­ily. It is ba­si­cally C# with im­mutabil­ity, vari­able and func­tion dec­la­ra­tion, and tu­ples took over from F#.

Let’s look on the code be­low and ex­plain some ba­sic state­ments:

operation Teleport(msg : Qubit, there : Qubit) : () { body { using (register = Qubit[1]) { // Ask for an auxillary qubit that we can use to prepare // for teleportation. let here = register[0]; // Create some entanglement that we can use to send our message. H(here); CNOT(here, there); // Move our message into the entangled pair. CNOT(msg, here); H(msg); // Measure out the entanglement. if (M(msg) == One) { Z(there); } if (M(here) == One) { X(there); } // Reset our "here" qubit before releasing it. Reset(here); } } }

C# has meth­ods and prop­er­ties, F# has func­tions and mem­bers and Q# has func­tions and op­er­a­tions. The op­er­a­tion dec­la­ra­tion starts with key­word, the name and ar­gu­ments fol­lows, and ends with the re­turn type. The ar­gu­ment name is first and the ar­gu­ment type is sec­ond. It is ex­actly like in F# and it fol­lows a hu­man think­ing rather than ma­chine pro­cess­ing needs.

The op­er­a­tion can be called from other .NET lan­guage. The func­tions are pure func­tions – they trans­form in­put val­ues to out­put val­ues without the pos­si­bil­ity of af­fecting any­thing else.

The qu­bit al­lo­ca­tion is tied with us­ing block. Op­er­a­tions are de­clared in a names­pace. When we open some names­pace, we can use op­er­a­tions from it. This is the same con­cept as C# us­ing key­word for ac­cess­ing all types from it. Op­er­a­tions H and CNOT are de­clared in Mi­crosoft.Quan­tum.Prim­i­tive names­pace.

The H func­tion will pro­duce a |0⟩ or |1⟩ with equal prob­a­bil­ity. It is like flip­ping a coin. The CNOT func­tion flips the sec­ond qu­bit when the first qu­bit is |1⟩. The M func­tion re­turns Zero or One whichever is clos­est. The Z func­tion is a Pauli Z gate. It ro­tates the vec­tor in Bloch sphere around Z-axis. The X func­tion is a Pauli X gate. It flips the vec­tor around X-axis. It is equi­va­lent to NOT gate for bit com­put­ers.

The Tele­por­ta­tion Sam­ple doesn’t trans­fer any in­for­ma­tion to an­other lo­ca­tion. It is an in-place test of the quan­tum tele­por­ta­tion al­gorithm im­ple­mented in Q#.

How to provide feedback

You can re­port prob­lems on GitHub. At this point Mi­crosoft is not ready to ac­cept Pull re­quests. You can also vote for or sub­mit sugges­tions on User­Voice.