Skip to content

Commit

Permalink
added support for Identity to all devices
Browse files Browse the repository at this point in the history
on Travis install PennyLane from git
expecting implementation of my PR ProjectQ-Framework/ProjectQ#288, updated the plugin to be compatible with upcoming versions of ProjectQ
  • Loading branch information
cgogolin committed Dec 4, 2018
1 parent e84cd59 commit 1d1f385
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
14 changes: 7 additions & 7 deletions pennylane_pq/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@
'T': TGate,
'SqrtX': SqrtXGate,
'SqrtSwap': SqrtSwapGate,
'Identity': Identity,
#operations/expectations of ProjectQ that do not work with PennyLane
#'AllPauliZ': AllZGate, #todo: enable when multiple return values are supported
#operations/expectations of PennyLane that do not work with ProjectQ
#'QubitStateVector': StatePreparation,
#In addition we support the Identity Expectation, but only as an expectation and not as an Operation, which is we we don't put it here.
}

class _ProjectQDevice(Device): #pylint: disable=abstract-method
Expand Down Expand Up @@ -291,8 +291,8 @@ class ProjectQSimulator(_ProjectQDevice):

short_name = 'projectq.simulator'
_operation_map = PROJECTQ_OPERATION_MAP
_expectation_map = {key:val for key, val in _operation_map.items()
if val in [XGate, YGate, ZGate]}
_expectation_map = dict({key:val for key, val in _operation_map.items()
if val in [XGate, YGate, ZGate]} , **{'Identity': Identity} )
_circuits = {}
_backend_kwargs = ['gate_fusion', 'rnd_seed']

Expand Down Expand Up @@ -416,7 +416,7 @@ class ProjectQIBMBackend(_ProjectQDevice):
if val in [HGate, XGate, YGate, ZGate, SGate, TGate,
SqrtXGate, SwapGate, Rx, Ry, Rz, R, CNOT,
CZ, Rot, BasisState]}
_expectation_map = {key:val for key, val in _operation_map.items() if val in [ZGate]}
_expectation_map = dict({key:val for key, val in _operation_map.items() if val in [ZGate]}, **{'Identity': Identity})
_circuits = {}
_backend_kwargs = ['use_hardware', 'num_runs', 'verbose', 'user', 'password', 'device',
'retrieve_execution']
Expand Down Expand Up @@ -460,7 +460,7 @@ def expval(self, expectation, wires, par):
expval = (1-(2*sum(p for (state, p) in probabilities.items() if state[wire] == '1'))-(1-2*sum(p for (state, p) in probabilities.items() if state[wire] == '0')))/2
#variance = 1 - ev**2
elif expectation == 'Identity':
expval = 1
expval = sum(p for (state, p) in probabilities.items())
# variance = 1 - expval**2
# elif expectation == 'AllPauliZ':
# expval = [((1-2*sum(p for (state, p) in probabilities.items()
Expand Down Expand Up @@ -506,8 +506,8 @@ class ProjectQClassicalSimulator(_ProjectQDevice):
short_name = 'projectq.classical'
_operation_map = {key:val for key, val in PROJECTQ_OPERATION_MAP.items()
if val in [XGate, CNOT, BasisState]}
_expectation_map = {key:val for key, val in PROJECTQ_OPERATION_MAP.items()
if val in [ZGate]}
_expectation_map = dict({key:val for key, val in PROJECTQ_OPERATION_MAP.items()
if val in [ZGate]}, **{'Identity': Identity})
_circuits = {}
_backend_kwargs = []

Expand Down
24 changes: 19 additions & 5 deletions pennylane_pq/pqops.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@
import numpy as np

class BasicProjectQGate(BasicGate): # pylint: disable=too-few-public-methods
"""Base class for ProjectQ gates defined here."""
"""Base class for ProjectQ gates."""
def __init__(self, name="unnamed"):
super().__init__()
self.name = name

def __str__(self):
return self.name

try:
from projectq.ops import MatrixGate
except:
MatrixGate = BasicGate

class BasicProjectQMatrixGate(MatrixGate): # pylint: disable=too-few-public-methods
"""Base class for ProjectQ gates defined via a matrix."""
def __init__(self, name="unnamed"):
super().__init__()
self.name = name
Expand Down Expand Up @@ -98,7 +112,7 @@ def __eq__(self, other):
if isinstance(other, self.__class__):
return self.angles == other.angles
return False

class QubitUnitary(BasicProjectQGate): # pylint: disable=too-few-public-methods
"""Class for the QubitUnitary gate.
Expand All @@ -108,7 +122,7 @@ class QubitUnitary(BasicProjectQGate): # pylint: disable=too-few-public-methods
do this here.
"""
def __new__(*par):
unitary_gate = BasicProjectQGate(par[0].__name__)
unitary_gate = BasicProjectQMatrixGate(par[0].__name__)
unitary_gate.matrix = np.matrix(par[1])
return unitary_gate

Expand Down Expand Up @@ -138,11 +152,11 @@ class Identity(BasicProjectQGate):
ProjectQ does not currently have a trivial identity gate,
so we provide a class for that here.
"""
def __init__(self):
def __init__(self, *par):
BasicProjectQGate.__init__(self, name=self.__class__.__name__)

def __or__(self, qubits):
pass

def __eq__(self, other):
return isinstance(other, self.__class__)

0 comments on commit 1d1f385

Please sign in to comment.