-
Notifications
You must be signed in to change notification settings - Fork 69
Quick start
As a test of QASM, consider a program that favors exactly one "on" bit out of five. This might be used as a building block for a larger program (e.g., a partitioning problem in which each bin contains exactly one out of five available objects). The truth table of acceptable solutions looks like this:
A | B | C | D | E |
---|---|---|---|---|
F | F | F | F | T |
F | F | F | T | F |
F | F | T | F | F |
F | T | F | F | F |
T | F | F | F | F |
The challenge is in representing that table in terms of weights and strengths. That's a subject for a separate discussion. For now, we show only how to describe the result of that computation in QASM format:
################################################
# QASM example: one "on" bit out of five total #
# By Scott Pakin <[email protected]> #
################################################
# Point weights
# A-E are the variables we care about.
# $a1-$a3 are ancillary variables.
A -2
B 1
C -2
D -1
E 1
$a1 0
$a2 4
$a3 3
# Coupler strengths
A B 2
A C 2
A D 2
A E 1
A $a1 1
A $a2 -4
A $a3 -4
B C 2
B D 2
B E 1
B $a1 1
B $a2 -4
B $a3 -1
C D 2
C E 3
C $a1 -1
C $a2 -4
C $a3 -4
D E 4
D $a1 -2
D $a2 -4
D $a3 -3
E $a1 -4
E $a2 0
E $a3 -4
$a1 $a2 -4
$a1 $a3 3
$a2 $a3 0
(Download as 1of5.qasm.)
Note that in addition to the truth-table variables A
-E
we employ three helper variables, $a1
, $a2
, and $a3
.
Prepare your environment for qasm
by setting the DW_INTERNAL__HTTPLINK
, DW_INTERNAL__SOLVER
, DW_INTERNAL__TOKEN
, and optionally, DW_INTERNAL__HTTPPROXY
environment variables. The dw
program from the qOp
package faciliates this. Run
$ dw get connection
$ dw get solver
and dw
will use your ~/.dwrc
file to set the environment properly. See the DW(1) and DWRC(5) manual pages for details.
Simply passing 1of5.qasm
to qasm
outputs a physical layout suitable for entry into D-Wave's Qubist interface:
$ qasm 1of5.qasm
By default, output goes to the standard output device. In addition, QASM produces a mapping from variables to physical qubit numbes on the standard error device, for example
# A --> 11 15 43
# B --> 39 47
# C --> 9 41
# D --> 8 12 40
# E --> 10 14 42
To actually run the program right from qasm
and report the results, simply add --run
to the command line:
$ qasm --run 1of5.qasm
# A --> 372 378 380
# B --> 375 379 383
# C --> 272 276 368
# D --> 275 371
# E --> 373 376 381
Solution #1 (energy = -22.25):
Name(s) Spin Boolean
------- ---- -------
A -1 False
B -1 False
C +1 True
D -1 False
E -1 False
While that answer is valid, the program was supposed to output all solutions found (over a default of 1000 samples). It turns out that the default chain strength—what links qubits 372 to 378 to 380 in the above, for example—is too weak. It defaults to the largest-in-magnitude strength (-4 in 1of5.qasm
). Bumping that down to -8, found by trial-and-error, gives us exactly the solution we're looking for:
$ qasm --chain=-8 --run 1of5.qasm
# A --> 738 742 750
# B --> 746 842 846
# C --> 744 840 844
# D --> 743 751
# E --> 745 841 845
Solution #1 (energy = -17.12):
Name(s) Spin Boolean
------- ---- -------
A -1 False
B -1 False
C -1 False
D -1 False
E +1 True
Solution #2 (energy = -17.12):
Name(s) Spin Boolean
------- ---- -------
A -1 False
B -1 False
C -1 False
D +1 True
E -1 False
Solution #3 (energy = -17.12):
Name(s) Spin Boolean
------- ---- -------
A -1 False
B -1 False
C +1 True
D -1 False
E -1 False
Solution #4 (energy = -17.12):
Name(s) Spin Boolean
------- ---- -------
A -1 False
B +1 True
C -1 False
D -1 False
E -1 False
Solution #5 (energy = -17.12):
Name(s) Spin Boolean
------- ---- -------
A +1 True
B -1 False
C -1 False
D -1 False
E -1 False
In the preceding examples, lines starting with "#
" are written to the standard error device, and the rest of the lines are written to the standard output device. Additional information about the execution—solver properties, mapping properties, timing data, solution characteristics, and more—can be written to the standard error device by providing the -v
(verbose) option to qasm
:
Specifying -v
twice writes even more information about the execution to the standard error device. This extended information additionally includes qasm
's internal, canonicalized representation of the input file; the complete set of solver properties, including a list of all available qubits and couplers; and a solution-energy histogram. Furthermore, all solutions are output, not just correct solutions. Within each solution, all variables are output, not just "interesting" variables (those without a "$
" in their name). Consequently, -v -v
(or -vv
for short) can be a handy debugging aid.