Q# Language Program-Defined Functions

The Q# programming language is designed for quantum computing. Learning any completely new programming language is difficult, but Q# is especially difficult because of all the underlying quantum concepts.

I was looking at some of the Q# online documentation examples and one of them used the Q# built-in library function BitSizeI(n) that computes the number of bits needed to store integer n. For example, if n = 13, then BitSizeI(n) = 4 because 3 bits can only store positive integers 0 to 7, but 4 bits can store positive integers 0 to 15.

In Q# there are operations that work on quantum qubits, and regular functions that work on:

Unit   - unsigned integer.
Int    - 64-bit signed integer.
BigInt - signed integer any size.
Double - 64-bit floating-point number.
Bool   - true or false.
String - sequence of UTF-16 characters.

I figured I’d investigate Q# regular functions by implementing a from-scratch version of a Bitsize(n) function. I launched the Visual Studio Code program and wrote this Q# program:

// functions.qs

import Microsoft.Quantum.Math.*; // BitSizeI()

function Main() : Int {
  let n : Int = 100;
  Message($"n = {n}");

  let nBits : Int = BitSize(n);
  Message($"nBits needed scratch function = {nBits}");

  let nBits = BitSizeI(n);
  Message($"nBits neededBitSizeI() = {nBits}");

  let x = 4.5;
  let y = 3.3;
  let sum = Sum(x, y);
  Message($"{x} + {y} = {sum}");

  return 0; // success
}

function Sum(x : Double, y : Double) : Double {
  let result : Double = x + y;
  return result;
}

function Power(x : Int, n : Int) : Int {
  // x^n
  mutable result : Int = x;
  for i in (1..n) {
    result *= x;
  }
  return result;
}

function BitSize(x : Int) : Int {
  // number bits needed to store x
  mutable result : Int = 1;
  while Power(2,result) "lt" x { // replace with symbol
    result += 1;
  }
  return result + 1;
}

I ran the program and the output was:

n = 100

nBits needed scratch function = 7

nBits neededBitSizeI() = 7

4.5 + 3.3 = 7.8

0
Finished shot 1 of 1

Q# simulation completed.

There are dozens of details in the demo program, but ultimately Q# program-defined functions have a lot in common with most C-family language functions. By the way, my code for the Power(x,n) and BitSize(x) functions is not efficient or practical — I was just exploring Q# function syntax.

At this point, it’s clear that learning the Q# language will take many months. I’m not sure if it’s worth the effort to put in all the time needed to completely master Q#, but because I spend 30 minutes each day before work to learn something new, I’ll probably keep looking at the Q# language, slowly but surely.



I am fluent in several programming languages but I struggled in school when I was learning German and Spanish and Latin. I’m amazed by people who can speak multiple languages. My sister Beth is fluent in several European languages. Many years ago, when I was a regional VP of research for a Fortune 500 company, my administrative assistant was a girl who effortlessly switched between Chinese (I can’t remember which dialect) and English. She told me she learned English a little bit at a time, mostly by watching English movies on TV when she was a young girl.


This entry was posted in Miscellaneous. Bookmark the permalink.

Leave a Reply