add __getitem__ to Register object, update examples

This commit is contained in:
Andrew W. Cross 2017-04-03 09:47:21 -04:00
parent 9606372e74
commit 281e83476a
5 changed files with 29 additions and 18 deletions

View File

@ -23,15 +23,19 @@ We want to reorganize the SDK so that it has a
comfortable and intuitive interface for developers. I will learn from
Ismael, Fran, Jorge, and Paco what architecture makes sense and try to
implement that architecture. This is meant as a place for us to try ideas
until we settle on something that makes sense for everyone. Right now, users
can create instances of *QuantumRegister* and *ClassicalRegister*, and bind
until we settle on something that makes sense for everyone.
Right now, users can create instances of *QuantumRegister* and *ClassicalRegister*, and bind
these to *QuantumCircuit*. They can then call methods of these objects to
apply gates within the circuit. The *extensions* directory extends these
objects as needed to support new gate sets and algorithms.
objects as needed to support new gate sets and algorithms. The "cswap" gate in
the standard extension shows how to build gates that are sequences of other
unitary gates. The Python file "header.py" shows how we append OPENQASM gate
definitions as we import extensions.
The *qiskit* directory is a Python
The *qiskit* directory is the main Python
module. It contains a *qasm* module for parsing OPENQASM circuits,
an *unroll* module for unrolling QASM to a circuit object, a *circuit* module
an *unroll* module to flatten QASM for a target gate basis, a *circuit* module
for representing, transforming, and computing properties of OPENQASM circuits
as directed acyclic graphs, and a *localize* module for mapping all-to-all
circuits to run on machines with fixed couplings.

View File

@ -36,3 +36,10 @@ class Register(object):
"""Check that j is a valid index into self."""
if j < 0 or j >= self.sz:
raise QISKitException("register index out of range")
def __getitem__(self, key):
"""Return tuple (self, key) if key is valid."""
if not isinstance(key, int):
raise QISKitException("expected integer index into register")
self.check_range(key)
return (self, key)

View File

@ -39,16 +39,16 @@ a.x(0) # a = 0...0001
b.x() # b = 1...1111
# Add a to b, storing result in b
majority(p, (cin, 0), (b, 0), (a, 0))
majority(p, cin[0], b[0], a[0])
for j in range(n-1):
majority(p, (a, j), (b, j+1), (a, j+1))
p.cx((a, n-1), (cout, 0))
majority(p, a[j], b[j+1], a[j+1])
p.cx(a[n-1], cout[0])
for j in reversed(range(n-1)):
unmajority(p, (a, j), (b, j+1), (a, j+1))
unmajority(p, (cin, 0), (b, 0), (a, 0))
unmajority(p, a[j], b[j+1], a[j+1])
unmajority(p, cin[0], b[0], a[0])
for j in range(n):
p.measure((b, j), (ans, j))
p.measure((cout, 0), (ans, n))
p.measure(b[j], ans[j])
p.measure(cout[0], ans[n])
print("QuantumCircuit OPENQASM")
print("-----------------------")

View File

@ -17,10 +17,10 @@ q.cx(1, 2)
q.barrier()
q.cx(0, 1)
q.h(0)
p.measure((q, 0), (c0, 0))
p.measure((q, 1), (c1, 0))
p.measure(q[0], c0[0])
p.measure(q[1], c1[0])
q.z(2).c_if(c0, 1)
q.x(2).c_if(c1, 1)
p.measure((q, 2), (c2, 0))
p.measure(q[2], c2[0])
print(p.qasm())

View File

@ -40,9 +40,9 @@ r.reset(0)
for i in range(n-1):
q.cx(i, i+1)
for i in range(n):
p.u1(math.pi / (i+1), (q, i))
p.h((q, i))
p.measure((q, i), (c, i))
p.u1(math.pi / (i+1), q[i])
p.h(q[i])
p.measure(q[i], c[i])
q.ccx(0, 1, 2)
print(p.qasm())
print(pp.qasm())