GithubHelp home page GithubHelp logo

dabacon / blqs-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ionq/blqs

0.0 1.0 0.0 497 KB

Python building blocks for quantum computing and domain specific languages.

License: Apache License 2.0

Jupyter Notebook 8.05% Python 91.67% Shell 0.27%

blqs-1's Introduction

blqs: Building Blocks for Domain Specific Languages

Continuous integration blqs: blqs PyPI version blqs_cirq: blqs_cirq PyPI version

Blqs (pronounced "blocks") is a framework for building (internal) domain specific languages in Python. It was inspired by TensorFlow's autograph library, and motivated by attempting to improve quantum computing programming frameworks like Cirq and Qiskit. If you want to write an domain specific language that uses native python code for "if" statements or "for" statements or "while" statements, blqs is for you!

Packages

This repo contains two packages:

Motivating example

Here is a motivating example. In many traditional quantum programming frameworks one writes a quantum program via appending to a container object, (here is an example from Cirq):

import cirq

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

In blqs instead one can define functions that return programs:

import blqs

H = blqs.Op('H')
CX = blqs.Op('CX')

@blqs.build
def hello_blqs():
    H(0)
    CX(0, 1)

program = hello_blqs()
print(program)
> prints
> H 0
> H 1

Here program is blqs.Program container of the listed blqs.Statements. The function annotation turns the hello_blqs function into a builder that, when called, returns the built program.

More interestingly, blqs programs can also take native python functionality, like if statements, and capture them in blqs objects:

M = blqs.Op('M')

@blqs.build
def hello_if_blqs():
    a = blqs.Register('a')
    H(0)
    M(0, 'a')
    if a:
        CX(0, 1)
    else:
        CX(1, 0)

program = hello_if_blqs()
print(program)
> prints
> H 0
> M 0, a
> if a:
>     CX 0, 1
> else:
>     CX 1, 0

# This is three statements, the last having captured the if.
for s in program:
    print(type(s))
> prints
> <class 'blqs.instruction.Instruction'>
> <class 'blqs.instruction.Instruction'>
> <class 'blqs.conditional.If'>

Further we can mix and match native and captured Python. For example, here a is just a normal python boolean variable, and we can use it in to build one of two different cases:

@blqs.build
def hello_native(a):
    if a:
        H(0)
    else:
        H(1)
print(hello_native(True))
> prints
> H 0
print(hello_native(False))
> prints
> H 1

Blqs is meant to be all about building programs and the intermediate representation of that program. In many ways it is meant to be a framework to help you build other frameworks. As such, every attempt will be made to keep blqs simple so that other frameworks can be built on top of it.

Contributing

We welcome contributions! In order to contribute we require that a Contributor License Agreement (CLA) be signed by the contributor or any organization (business, school) that retains rights to your contributed code. To get setup with a development environment and for info on the CLA see here.

License

Both blqs and blqs_cirq are licensed under Apache 2.0.

blqs-1's People

Contributors

dabacon avatar mjk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.