Introducing IonQ Forte, The First Software-Configurable Quantum Computer

Learn more about our latest system

Start Building With blqs

In the very early days of classical computers (think room-sized vacuum tube computers), users wrote programs directly in the binary format understood by their machine, in machine code. Such machine code was fine when programs were small and of limited complexity, but as Moore’s law increased the size of programs one could write, higher level programming languages were developed. These allowed code to be more easily written and read, added better abstractions for programming, and supported automatic compilation for increased performance.

Today’s quantum computer programs, with qubit counts from the tens to hundreds, are just beginning to be large enough that one wants to develop easier ways to write these programs than something that looks like quantum machine code. How will quantum programming rise to this challenge? Today we are happy to announce a piece of software that we believe can help with this question, an open-source Python library called blqs (pronounced “blocks”), now available on GitHub.

In quantum computing there are two main approaches to programming quantum computers: build on top of an existing language or build a new programming language. The first path is taken by, for example, IBM’s Qiskit and Google’s Cirq, where one writes a quantum computer program by writing Python describing how to build the quantum program. The second path, of an entirely new language, is, for example, taken by Microsoft’s Q# language. The benefit of the first approach is that users can leverage the existing tools and libraries of the language the framework is built on. Python, for instance, has a rich array of tools, like NumPy for numeric computation, or Matplotlib for plotting data. Users of Qiskit and Cirq can stay in one language— Python—to write a program, run it, and then analyze its results. The drawback to this approach is that because it is not its own language, one has to shoehorn the code one writes into the base language, for example into Python.

This is where blqs comes in.

blqs is a Python framework designed to enable one to build Python frameworks that let one write Python-like code for quantum programs (or any other domain specific language you like). It’s perhaps easiest to see how this works by demonstration.

Here is a typical program written in Python using the Cirq framework:

 import cirq

  circuit = cirq.Circuit()
  q0, q1 = cirq.LineQubit.range(2)
  circuit.append(cirq.CX(q0, q1))

In this code we 1) create a circuit object, 2) create a few qubits, 3) start adding gates acting on qubits to the circuit object. The circuit object then holds our quantum program and we can do things like pass it off to a simulator to simulate the program, or send it to a quantum computer to run via a cloud API. Notice that the circuit sort of behaves like a simple container, and the code we write for the quantum program isn’t much like the normal code we write a program in. blqs is designed to get around this pattern. In blqs one would write something like:

 import blqs

  h = blqs.Op('H')
  cx = blqs.Op('CX')
  def hello_blqs():
      cx(0, 1)

  circuit = hello_blqs()

Now we write our program in an annotated function, and when we call this function it creates a program object. Notice that we now write our code in a more imperative style—that is, instead of appending onto a circuit, we just write the instructions one after another.

Just having an imperative way of writing circuits has been done in other frameworks, but where blqs gets really interesting is if you want to be able to capture native Python in a program. For example, suppose that you want to do conditional operations. In current frameworks you have to do something rather gnarly like (example from Cirq):

circuit.measure(q0, “x”)

where we have applied the gate H conditional on the measurement result preceding it. In blqs one can instead write this in a more Pythonic manner

  M = blqs.Op('M')
  def example():
      x = blqs.Register('x')
      M(0, x)
      if x:

  circuit = example()

Notice here that we have used Python’s native if statement. In effect, blqs allows one to overload the if statement.

Behind the scenes, blqs uses code generation. It works by inspecting the code you’ve written in the annotated function, rewriting it to capture the intent of the code into a Python object. This was inspired by TensorFlow’s autograph, now a standard part of TensorFlow 2.0, which had the similar problem of trying to write TensorFlow graphs in a more imperative and Pythonic style.

blqs itself is not a quantum programming framework, but instead is a framework upon which one can build quantum programming frameworks. It can also be back-ported to existing frameworks. As an example of this we have implemented blqs_cirq, which allows one to write Cirq code in the imperative form. Below, for example, we show how to use blqs_cirq to write a circuit with two moments:

  import cirq
  import blqs_cirq as bc

  def old_way():
      q0, q1 = cirq.LineQubit.range(2)
      circuit = cirq.Circuit()
      m1 = cirq.Moment([cirq.H(q0)])
      m2 = cirq.Moment([cirq.CX(q0, q1)])
      return circuit

  old_circuit = old_way()
  def new_way():
      with bc.Moment():
      with bc.Moment():
          bc.CX(0, 1)

  new_circuit = new_way)

There is an inherent tension in quantum programming today. On the one hand one would like to be able to write new quantum programming languages that best match the subtle ways that quantum computers differ from classical. On the other hand, forcing a new language onto developers is hard, especially when the developers have to switch to different languages to analyze, run, or simulate the quantum program.

blqs, we hope, is a bridge between these two, allowing one to write quantum program languages in Python that are more expressive and more like normal Python. Eventually new quantum programming languages will certainly dominate, but in the meantime, we hope that you will have fun playing with blqs.

Visit blqs on GitHub to get started!