GithubHelp home page GithubHelp logo

rigetti / reference-qvm Goto Github PK

View Code? Open in Web Editor NEW
45.0 14.0 19.0 304 KB

A reference implementation for a quantum virtual machine in Python

License: Apache License 2.0

Python 100.00%
quantum-simulation forest wavefunction quil

reference-qvm's Introduction

Build Status

Reference QVM

The referenceqvm is the reference implementation of the QVM outlined in the arXiv:1608:03355 by Robert Smith, Spike Curtis, and Will Zeng. It is a research package that supports rapid prototyping and development of quantum programs using pyQuil.

Currently, this QVM supports a subset of functionality in the Quil specifications, excepting certain functions (DEFCIRCUIT, WAIT, NOP).

Noise models (dephasing, Kraus operators), parametrization with bits in classical memory, and other features will be added in future releases.

Installation

You can install reference-qvm directly from the Python package manager pip using:

pip install referenceqvm

To instead install reference-qvm from source, clone this repository, cd into it, and run:

pip install -r requirements.txt -e .

This will install the reference-qvm's dependencies if you do not already have them.

Development and Testing

We use tox and pytest for testing. Tests can be executed from the top-level directory by simply running:

tox

The setup is currently testing Python 2.7 and Python 3.6.

Building the Docs

We use sphinx to build the documentation. To do this, navigate into pyQuil's top-level directory and run:

sphinx-build -b html ./docs/source ./docs/build

To view the docs navigate to the newly-created docs/build directory and open the index.html file in a browser. Note that we use the Read the Docs theme for our documentation, so this may need to be installed using pip install sphinx_rtd_theme.

Interaction with the referenceqvm

The qvm can be accessed in a similar way to the Forest QVM access. Start by importing the synchronous connection object from the referenceqvm.api module

from referenceqvm.api import QVMConnection

and initialize a connection to the reference-qvm

qvm = QVMConnection()

By default, the Connection object uses the wavefunction transition type.

Then call the qvm.wavefunction(prog) method to get back the classical memory and the pyquil.Wavefunction object given a pyquil.quil.Program object prog.

The reference-qvm has the same functionality as Forest QVM and is useful for testing small quantum programs on a local machine. For example, the same code (up to the referenceqvm.api import) can be used to simulate pyquil programs.

>>> import pyquil.quil as pq
>>> import referenceqvm.api as api
>>> from pyquil.gates import *
>>> qvm = api.QVMConnection()
>>> p = pq.Program(H(0), CNOT(0,1))
<pyquil.pyquil.Program object at 0x101ebfb50>
>>> qvm.wavefunction(p)[0]
[(0.7071067811865475+0j), 0j, 0j, (0.7071067811865475+0j)]

QVMConnection can also initialize a QVM that does not return a wavefunction but instead a unitary corresponding to the pyquil program. This can be extremely useful in terms of debugging and understanding gate physics. For example, we can examine the unitary for a CNOT operator.

>>> import pyquil.quil as pq
>>> import referenceqvm.api as api
>>> from pyquil.gates import CNOT
>>> qvm = api.QVMConnection(type_trans='unitary')
>>> p = pq.Program(CNOT(1, 0))
>>> u = qvm.unitary(p)
>>> print(u)
[[ 1.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  1.+0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  1.+0.j]
 [ 0.+0.j  0.+0.j  1.+0.j  0.+0.j]]

How to cite the reference-qvm

If you use the reference-qvm please cite the repository as follows:

bibTex:

@misc{rqvm2017.0.0.1,
  author = {Rigetti Computing",
  title = {Reference-QVM},
  year = {2017},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/rigetticomputing},
  commit = {the commit you used}
}

and the paper outlining the Mathematical specification of the quantum-abstract-machine:

bibTeX:

@misc{1608.03355,
  title={A Practical Quantum Instruction Set Architecture},
  author={Smith, Robert S and Curtis, Michael J and Zeng, William J},
  journal={arXiv preprint arXiv:1608.03355},
  year={2016}
}

reference-qvm's People

Contributors

ejdanderson avatar jotterbach avatar ncrubin avatar stevenheidel avatar thomdixon avatar willzeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reference-qvm's Issues

pyquil.quilbase in qvm_wavefunction.py cannot find Instr module

I recently installed referenceqvm via pip for python 3.6.3. When I try to run the example on your website, I get the following error:

>>> import referenceqvm.api as api
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/gpfs0/home/gdhpcgroup/user/local/pip-installs/python-3.6.3/lib/python3.6/site-packages/referenceqvm/api.py", line 5, in <module>
    from referenceqvm.qvm_wavefunction import QVM_Wavefunction
  File "/gpfs0/home/gdhpcgroup/user/local/pip-installs/python-3.6.3/lib/python3.6/site-packages/referenceqvm/qvm_wavefunction.py", line 45, in <module>
    from pyquil.quilbase import (Instr,
ImportError: cannot import name 'Instr'

It seems that the ‘Instr’ does not exist. Advice?

TypeError when printing wavefunction

Printing out the wavefunction using the Reference QVM throws a type error as pyQuil attempts to perform various scalar math operations.

For example:

from referenceqvm import api
import pyquil.quil as pq
from pyquil.gates import *

qvm = api.SyncConnection()

p = pq.Program(H(0), CNOT(0,1))
prog = pq.Program().inst(H(0)).measure(0, [0]).measure(0, [1])
result = qvm.run(prog, [0, 1])

for index, amplitude in enumerate(qvm.wavefunction(p)[0]):
    print(amplitude)
    print(amplitude.real)
    print('--')
    
print(qvm.wavefunction(p)[0])

produces the following output :

[ 0.70710678+0.j]
[ 0.70710678]
--
[ 0.+0.j]
[ 0.]
--
[ 0.+0.j]
[ 0.]
--
[ 0.70710678+0.j]
[ 0.70710678]
--
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-8e1890fb773d> in <module>()
     14     print('--')
     15 
---> 16 print(qvm.wavefunction(p)[0])

/usr/local/lib/python3.6/site-packages/pyquil/wavefunction.py in __str__(self)
     29 
     30     def __str__(self):
---> 31         return self.pretty_print(decimal_digits=10)
     32 
     33     def get_outcome_probs(self):

/usr/local/lib/python3.6/site-packages/pyquil/wavefunction.py in pretty_print(self, decimal_digits)
     79         for index, amplitude in enumerate(self):
     80             outcome = get_bitstring_from_index(index, qubit_num)
---> 81             amplitude = round(amplitude.real, decimal_digits) + \
     82                 round(amplitude.imag, decimal_digits) * 1.j
     83             if amplitude != 0.:

TypeError: type numpy.ndarray doesn't define __round__ method

unitary_generator.py : tensor_gates() tries to call numpy array

I have a minimal working example:

from pyquil.quil import Program
from pyquil.gates import *
from pyquil.parameters import Parameter, quil_sin, quil_cos
from pyquil.quilbase import DefGate
from pyquil.api import QVMConnection
#from referenceqvm.api import QVMConnection
import numpy as np
theta = Parameter('theta')
cry = np.array([[1.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0],[0.0,0.0,quil_cos(theta / 2), -1 * quil_sin(theta / 2)],[0.0,0.0,quil_sin(theta / 2), quil_cos(theta / 2)]]) 
dg = DefGate('CRY', cry, [theta])
CRY = dg.get_constructor()
p = Program()
p.inst(dg)
p.inst (X(0))
p.inst (X(1))
p.inst (CRY(4.304)(0,2))
qvm = QVMConnection()
wf = qvm.wavefunction(p)
print(wf)

When I run this I get the output :

(-0.5490303672+0j)|011> + (0.8358024024+0j)|111>

If I switch from the Rigetti server to a local installation of reference-qvm (i.e. comment from pyquil.api import QVMConnection and uncomment #from referenceqvm.api import QVMConnection) and try to run it I get the error :

Traceback (most recent call last):
  File "minimal_broken_example.py", line 22, in <module>
    wf = qvm.wavefunction(p)
  File "/gpfs0/export/opt/python/3.6.5/additional_packages/src/reference-qvm/referenceqvm/qvm_wavefunction.py", line 378, in wavefunction
    self.kernel()
  File "/gpfs0/export/opt/python/3.6.5/additional_packages/src/reference-qvm/referenceqvm/qam.py", line 167, in kernel
    halted = self.transition(self.current_instruction())
  File "/gpfs0/export/opt/python/3.6.5/additional_packages/src/reference-qvm/referenceqvm/qvm_wavefunction.py", line 268, in transition
    self._transition(instruction)
  File "/gpfs0/export/opt/python/3.6.5/additional_packages/src/reference-qvm/referenceqvm/qvm_wavefunction.py", line 180, in _transition
    unitary = tensor_gates(self.gate_set, self.defgate_set, instruction, self.num_qubits)
  File "/gpfs0/export/opt/python/3.6.5/additional_packages/src/reference-qvm/referenceqvm/unitary_generator.py", line 352, in tensor_gates
    (*[value_get(p) for p in pyquil_gate.params]),
TypeError: 'numpy.ndarray' object is not callable

It seems to me that the in the function tensor_gates() that it is expecting something other than a numpy array to be returned by dict_check[pyquil_gate.name].

QUESTIONS:

  1. How do I fix this?
  2. Also, is the expected behavior of the local installation of reference-qvm supposed to exactly replicate the behavior of the Rigetti server version?

Thanks

Installation by pip fails

The README suggests installing using pip by:

pip install reference-qvm

but this fails with the error message:

Collecting reference-qvm
  Could not find a version that satisfies the requirement reference-qvm (from versions: )
No matching distribution found for reference-qvm

Installing referenceqvm works fine though.

The name in setup.py should probably be updated.

#1 was a similar issue.

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.