rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.41k stars 342 forks source link

Returning Instructions #136

Closed rasa97 closed 7 years ago

rasa97 commented 7 years ago

I created a program like this:

ins = pq.Program().inst(bell_state('00')) The Bell state function is defined as:

def bell_state(a):
    if a == '00':
        return 'I(1), H(1), I(2), CNOT(1,2)'
    if a == '01':
        return 'I(1), H(1), X(2), CNOT(1,2)'
    if a == '10':
        return 'X(1), H(1), I(0), CNOT(1,2)'
    if a == '11':
        return 'X(1), H(1), X(0), CNOT(1,0)'

First I tried without quotes. It raised: TypeError: op must be a string

Then after providing the quotes:

! Server caught an error. This could be due to a bug in the server
! or a bug in your code. The server came back with the following
! information:
================================================================================
Unexpected terminal :RIGHT-PAREN (value NIL). Expected one of: (:EXPT :DIVIDE
                                                                :TIMES :MINUS
                                                                :PLUS NIL)
================================================================================
! If you suspect this to be a bug in pyQuil or Rigetti Forest,
! then please describe the problem in a GitHub issue at:
!
!      https://github.com/rigetticomputing/pyquil/issues

Traceback (most recent call last):
  File "one.py", line 62, in <module>
    results = qvm.run(measures,  [0,1])
  File "/Library/Python/2.7/site-packages/pyquil/api.py", line 428, in run
    res = self.post_job(payload, headers=self.json_headers)
  File "/Library/Python/2.7/site-packages/pyquil/api.py", line 492, in post_job
    return self.post_json(program, headers)
  File "/Library/Python/2.7/site-packages/pyquil/api.py", line 261, in post_json
    res.raise_for_status()
  File "/Library/Python/2.7/site-packages/requests/models.py", line 935, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://api.rigetti.com/qvm

Anyway around this?

stevenheidel commented 7 years ago

Your quil instructions aren't formatted correctly. It should be: 'I 1\nH 1\nI 2\nCNOT 1 2' which went printed looks like this:

I 1
H 1
I 2
CNOT 1 2

One instruction per line, no parenthesis around the gates. Better is to pass it in as an array: Program(['I 1', 'H 1', 'I 2'])

Even better still is to use the actual gate objects defined in pyquil:

from pyquil.gates import *

p = Program()
p.inst(I(0))
p.inst(H(1))

or Program([I(0), H(1)])