jsquil

Replace pyQuil library with NodeJS


Keywords
quantum, quantum-computing
License
Apache-2.0
Install
npm install jsquil@2.1.1

Documentation

jsquil

Greenkeeper badge

JavaScript interface for writing Quil programs, based on Rigetti Computing's pyQuil package.

Make a list of instructions to run on a hybrid computer with both qubits and classical registers, and then use the measure instruction to store a qubit value onto a classical register.

You can then return the value of these classical registers on each run of your program.

Want more JS and Quantum Computers?

Upgrade to Quantum Peep

Multi-platform, async quantum computing library written in TypeScript

Sample code

Tests based on the example code in pyQuil

import { gates, inits, operations, Program, QVM, Connection } from 'jsquil'

// request API credentials from http://rigetti.com/forest
let c = new Connection({
  user_id: 'USER_ID',
  api_key: 'API_KEY'
});

// connection for QVM Docker container (which I host)
let c2 = new Connection({
  user_id: 'USER_ID',
  api_key: 'API_KEY'
}, 'http://165.227.62.245:5000');

let q = new QVM(c2);

let p = new Program();
// put an X gate on the zeroth qubit
p.inst(gates.X(0));

// store the zeroth qubit's value in the first classical register
p.measure(0, 1);

// p now contains Quil instructions, which look like this:
// p.code()
// >  DECLARE ro BIT[2]
// >  X 0
// >  MEASURE 0 ro[1]

// run the program twice, returning classical registers from each iteration
q.run(p, 2, (err, returns) => {
  // err = null
  // returns = [[1], [1]]
});

Changing the run command to execute a program ten times:

q.run(p, 10, (err, returns) => { });

Two ways to write a series of gate commands:

p.inst(gates.X(0), gates.Y(1), gates.Z(0));
// same as
p.inst(gates.X(0));
p.inst(gates.Y(1));
p.inst(gates.Z(0));

p.code();
> "X 0\nY 1\nZ 0\n"

Quantum Fourier Transform:

p.inst(
  gates.H(2),
  gates.CPHASE(Math.PI / 2, 1, 2),
  gates.H(1),
  gates.CPHASE(Math.PI / 4, 0, 2),
  gates.CPHASE(Math.PI / 2, 0, 1),
  gates.H(0),
  gates.SWAP(0, 2)
);

Initializing a classical register value

p.inst( inits.TRUE(0) );

Operations on classical registers

p.inst(operations.EXCHANGE(0, 1));
// others: NOT, AND, OR, MOVE

Reset, wait, and halt commands:

p.reset();
p.wait();
p.halt();

Looping some instructions while a classical register value is TRUE

let classical_register = 2;
let loop_program = new Program();
loop_program.inst(gates.X(0), gates.H(0));
loop_program.measure(0, classical_register);
p.while_do(classical_register, loop_program);

An if-then-else statement combines multiple program objects and chooses one based on a classical register bit value

let then_branch = new Program();
...
let else_branch = new Program();
...
p.if_then(test_register, then_branch, else_branch);

Adding gate and measurement noise to the QVM, to simulate a quantum computer

let gate_noise = [x, y, z];
let measure_noise = [0.2, 0, 0];
let q = new QVM(connection, gate_noise, measure_noise);

Endpoints

If the endpoint changes:

let c = new Connection({
  user_id: 'USER_ID',
  api_key: 'API_KEY'
}, 'https://endpoint.example.com');

Tests

npm install mocha -g
npm test

License

Apache license (same as pyQuil)