Skip to content
This repository was archived by the owner on Oct 7, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion referenceqvm/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""
Sets up the appropriate QVM, and interfacing.
"""
from referenceqvm.gates import gate_matrix
from referenceqvm.gates import gate_matrix, stabilizer_gate_matrix
from referenceqvm.qvm_wavefunction import QVM_Wavefunction
from referenceqvm.qvm_unitary import QVM_Unitary
from referenceqvm.qvm_density import QVM_Density
from referenceqvm.qvm_stabilizer import QVM_Stabilizer


def QVMConnection(type_trans='wavefunction',
Expand Down Expand Up @@ -35,6 +36,8 @@ def QVMConnection(type_trans='wavefunction',
elif type_trans == 'density':
qvm = QVM_Density(gate_set=gate_set, noise_model=noise_model)

elif type_trans == 'stabilizer':
qvm = QVM_Stabilizer(gate_set=stabilizer_gate_matrix)
else:
raise TypeError("{} is not a valid QVM type.".format(type_trans))

Expand Down
10 changes: 10 additions & 0 deletions referenceqvm/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ def BARENCO(alpha, phi, theta):
'CZ': CZ
}

stabilizer_gate_matrix = {
'I': I,
'X': X,
'Y': Y,
'Z': Z,
'H': H,
'CNOT': CNOT,
'S': S,
'CZ': CZ
}

# noisy gates
def relaxation(p):
Expand Down
38 changes: 35 additions & 3 deletions referenceqvm/qam.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
QAM superclass. Implements state machine model, program loading and processing,
kernel - leaving details of evolution up to subclasses.
"""
import sys
import numpy as np

from referenceqvm.unitary_generator import value_get
Expand All @@ -26,7 +27,8 @@
from pyquil.quilbase import (Gate,
Measurement,
UnaryClassicalInstruction,
BinaryClassicalInstruction)
BinaryClassicalInstruction,
Label, JumpTarget)


class QAM(object):
Expand Down Expand Up @@ -91,8 +93,7 @@ def load_program(self, pyquil_program):

# NOTE: all_inst is set by the subclass
if invalid is True and self.all_inst is False:
raise TypeError("In QVM_Unitary, only Gates and DefGates are "
"supported")
raise TypeError("Some gates used are not allowed in this QAM")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will defgates let the user sneak through invalid programs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but that was always a risk of defgates that was weighed in the original quil spec. If users want to use defgate they should know that it takes them outside of the QAM model. Remember QAM requires gate specification in the definition. If the user wants to add something to this list then we are trusting them to know what they're doing.


# set internal program and counter to their appropriate values
self.program = pyquil_program
Expand All @@ -105,6 +106,17 @@ def load_program(self, pyquil_program):
self.num_qubits = q_max
self.classical_memory = np.zeros(c_max).astype(bool)

def memory_reset(self):
if self.program is None:
raise TypeError("Program must be loaded to call reset")

# setup quantum and classical memory
q_max, c_max = self.identify_bits()
if c_max <= 512: # allocate at least 512 cbits (as floor)
c_max = 512
self.num_qubits = q_max
self.classical_memory = np.zeros(c_max).astype(bool)

def identify_bits(self):
"""
Iterates through QAM program and finds number of qubits and cbits
Expand Down Expand Up @@ -168,6 +180,26 @@ def kernel(self):
if halted:
break

def find_label(self, label):
"""
Helper function that iterates over the program and looks for a
JumpTarget that has a Label matching the input label.

:param Label label: Label object to search for in program

:return: program index where Label is found
:rtype: int
"""
assert isinstance(label, Label)
for index, action in enumerate(self.program):
if isinstance(action, JumpTarget):
if label == action.label:
return index

# Label was not found in program.
raise RuntimeError("Improper program - Jump Target not found in the "
"input program!")

def transition(self, instruction):
"""
Abstract class for the transition type
Expand Down
Loading