Page 35 - MSDN Magazine, September 2019
P. 35
Getting Started with Q#
Q# language support for Visual Studio comes with the Microsoft Quantum Development Kit. The easiest way to install the kit for Visual Studio is to use the Visual Studio Extension Manager.
From the Extensions menu in Visual Studio 2019, select Manage ExtensionstodisplaytheManageExtensionsdialog.Enter“quantum” into the search box to locate the kit. Once it’s installed, you can cre- ate new Q# project types, Q# syntax highlighting is enabled and debugging of Q# projects works as you’d expect.
Q# is a procedural, domain-specific language, syntactically influenced by C# and F#. Q# files contain operations, functions and custom type definitions, the names of which must be unique within a namespace, as shown in Figure 1. Members of other namespaces can be made available using the “open” directive.
Functions are analogous to functions or methods in other pro- cedural languages. A function accepts zero or more arguments and may return a single object, value or tuple. Each parameter to a function consists of a variable name followed by a colon and then its type. The return value for a function is specified after a colon, at the end of the parameter list.
Recall from Part 3 of my “Quantum Computation Primer” (tinyurl.com/controlledgates) that controlled-U gates allow you to add a control input for an arbitrary quantum gate. Q# has a Controlled keyword for this purpose, which allows you to apply a control to an operator, for example:
Controlled X([controlQubit1, controlQubit2], targetQubit)
Here, the Controlled statement places two control inputs on the Pauli-X gate.
Operations share the same traits as functions, but operations can also represent unitary operators. You can think of them as composite quantum gates. They allow you to define what happens when they’re used as part of a controlled operation.
In addition, operations allow you to explicitly define the reverse of the operation (via the adjoint keyword). Adjoint is the complex conjugate transpose of the operator. For more information, see tinyurl.com/brafromket.
The newtype keyword allows you to define a custom type, which you can then use in operations and functions.
Let’s now examine some of the most common expression types.
Figure 1 Functions, Operations and Custom Type Definitions
Variables are immutable in Q# by default. To declare a new vari- able, use the let keyword, like so:
let foo = 3;
The compiler infers the type of the variable from the value you assign it. If you need to change a variable’s value, use the mutable keywordwhendeclaringit:
mutable foo = 0; set foo += 1;
To create an immutable array in Q#, use the following:
let immutableArray = [11, 21, 3, 0];
An array in Q# is a value type and is, for the most part, immutable, even when created using the mutable keyword. When the mutable keyword is used, you can assign another array to the identifier, but can’t replace items in the array:
mutable foo = new Int[4]; set foo = foo w/ 0 <- 1;
The w/ operator is an abbreviation of the word “with.” On the second line, all the values in foo are copied to a new array, with the first item (index 0) in the array set to 1. It has a O(n) runtime com- plexity. The previous assignment statement can be expressed more succinctly by combining it with an equal sign, like so:
set foo w/= 0 <- 1;
When the w/= operator is used, where possible an in-place replacement is performed, reducing the runtime complexity to O(1).
You can create a loop by using a for statement:
for (i in 1..10) {
Message($”i={i},”); }
The built-in Message function writes messages to the console. Q# supports string interpolation. By prepending a “$” to a string literal, you can place expressions in curly brackets.
To loop over a collection, use a for statement, like so:
for (qubit in qubits) // Qubits is an array of Qubits {
H(qubit); // Apply a Hadamard gate to the Qubit }
This was a whirlwind tour of Q#. For more in-depth coverage, please visit tinyurl.com/qsharp. I also recommend working through the Microsoft Quantum Katas (tinyurl.com/quantumkatas).
Implementing Superdense Coding with Q#
Now let’s look at implementing the Quantum superdense coding protocol in Q#. I’m assuming you’ve read the first and second part of my series or already have an understanding of Dirac notation and quantum computation fundamentals.
In the opening description of superdense coding, recall that Char- lie entangles a pair of qubits and sends one to Alice and one to Bob. To entangle a pair of qubits, which start off in a |0〉 state, you send the first qubit through a Hadamard gate, which changes its state to:
|+〉 = |01〉 + |11〉 √2
The Hadamard gate places the first qubit in a superposition of |0〉 and |1〉, sending it to the |+〉 state. Here’s the code:
operation CreateEntangledPair(qubit1 : Qubit, qubit2 : Qubit) : Unit {
H(qubit1);
CNOT(qubit1, qubit2); }
namespace Quantum.SuperdenseCoding {
open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Canon;
function SayHellow(name : String) : String {
return “Hello “ + name; }
operation EntangledPair(qubit1 : Qubit, qubit2 : Qubit) : Unit is Ctl + Adj
{
H(qubit1); CNOT(qubit1, qubit2);
}
newtype IntTuple = (Int, Int); }
msdnmagazine.com
September 2019 27