Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
TimeEvolution(time=2.0, hamiltonian=hamiltonian) | wavefunction
Args:
time(float, int): time t
hamiltonian(QubitOperator): hamiltonaian H
Returns:
Instance of ProjectQ TimeEvolution gate.
Raises:
TypeError: If time is not a numeric type and hamiltonian is not a
QubitOperator.
NotHermitianOperatorError: If the input hamiltonian is not
hermitian (only real coefficients).
"""
projectq_qubit_operator = projectq.ops.QubitOperator()
for term, coefficient in hamiltonian.terms.items():
projectq_qubit_operator.terms[term] = coefficient
return projectq.ops.TimeEvolution(time, projectq_qubit_operator)
def expval(self, observable, wires, par):
"""Retrieve the requested observable expectation value.
"""
if observable == 'PauliX' or observable == 'PauliY' or observable == 'PauliZ':
expval = self._eng.backend.get_expectation_value(
pq.ops.QubitOperator(str(observable)[-1]+'0'),
[self._reg[wires[0]]])
elif observable == 'Hadamard':
expval = self._eng.backend.get_expectation_value(
1/np.sqrt(2)*pq.ops.QubitOperator('X0')+1/np.sqrt(2)*pq.ops.QubitOperator('Z0'),
[self._reg[wires[0]]])
elif observable == 'Identity':
expval = 1
# elif observable == 'AllPauliZ':
# expval = [self._eng.backend.get_expectation_value(
# pq.ops.QubitOperator("Z"+'0'), [qubit])
# for qubit in self._reg]
if not self.analytic and observable != 'Identity':
p0 = (expval+1)/2
p0 = max(min(p0, 1), 0)
n0 = np.random.binomial(self.shots, p0)
expval = (n0 - (self.shots-n0)) / self.shots
return expval
For consistency we define this class, whose constructor is made to retun
a gate with the correct properties by overwriting __new__().
"""
def __new__(*par): # pylint: disable=no-method-argument
return pq.ops.Tensor(pq.ops.ZGate())
#gates
H = Gate('H', 1, 0, pq.ops.HGate)
X = Gate('X', 1, 0, pq.ops.XGate)
Y = Gate('Y', 1, 0, pq.ops.YGate)
Z = Gate('Z', 1, 0, pq.ops.ZGate)
S = Gate('S', 1, 0, pq.ops.SGate)
T = Gate('T', 1, 0, pq.ops.TGate)
SqrtX = Gate('SqrtX', 1, 0, pq.ops.SqrtXGate)
Swap = Gate('Swap', 2, 0, pq.ops.SwapGate)
SqrtSwap = Gate('SqrtSwap', 2, 0, pq.ops.SqrtSwapGate)
#Entangle = Gate('Entangle', n, 0, pq.ops.EntangleGate) #This gate acts on all qubits
#Ph = Gate('Ph', 0, 1, pq.ops.Ph) #This gate acts on all qubits or non, depending on how one looks at it...
Rx = Gate('Rx', 1, 1, pq.ops.Rx) #(angle) RotationX gate class
Ry = Gate('Ry', 1, 1, pq.ops.Ry) #(angle) RotationY gate class
Rz = Gate('Rz', 1, 1, pq.ops.Rz) #(angle) RotationZ gate class
R = Gate('R', 1, 1, pq.ops.R) #(angle) Phase-shift gate (equivalent to Rz up to a global phase)
#pq.ops.AllGate , which is the same as pq.ops.Tensor, is a meta gate that acts on all qubits
#pq.ops.QFTGate #This gate acts on all qubits
#pq.ops.QubitOperator #A sum of terms acting on qubits, e.g., 0.5 * ‘X0 X5’ + 0.3 * ‘Z1 Z2’
CRz = Gate('CRz', 2, 1, pq.ops.CRz) #(angle) Shortcut for C(Rz(angle), n=1).
CNOT = Gate('CNOT', 2, 0, CNOTClass)
CZ = Gate('CZ', 2, 0, CZClass)
#Toffoli = Gate('Toffoli', 3, 0, ToffoliClass)
#pq.ops.TimeEvolution #Gate for time evolution under a Hamiltonian (QubitOperator object).
AllZ = Gate('AllZ', 1, 0, AllZClass) #todo: 1 should be replaced by a way to specify "all"
def __or__(self, qubits):
pq.ops.Rz(self.angles[0]) | qubits #pylint: disable=expression-not-assigned
pq.ops.Ry(self.angles[1]) | qubits #pylint: disable=expression-not-assigned
pq.ops.Rz(self.angles[2]) | qubits #pylint: disable=expression-not-assigned
def __or__(self, qubits):
for i, qureg in enumerate(qubits):
if self.basis_state_to_prep[i] == 1:
pq.ops.XGate() | qureg #pylint: disable=expression-not-assigned
Ry = Gate('Ry', 1, 1, pq.ops.Ry) #(angle) RotationY gate class
Rz = Gate('Rz', 1, 1, pq.ops.Rz) #(angle) RotationZ gate class
R = Gate('R', 1, 1, pq.ops.R) #(angle) Phase-shift gate (equivalent to Rz up to a global phase)
#pq.ops.AllGate , which is the same as pq.ops.Tensor, is a meta gate that acts on all qubits
#pq.ops.QFTGate #This gate acts on all qubits
#pq.ops.QubitOperator #A sum of terms acting on qubits, e.g., 0.5 * ‘X0 X5’ + 0.3 * ‘Z1 Z2’
CRz = Gate('CRz', 2, 1, pq.ops.CRz) #(angle) Shortcut for C(Rz(angle), n=1).
CNOT = Gate('CNOT', 2, 0, CNOTClass)
CZ = Gate('CZ', 2, 0, CZClass)
#Toffoli = Gate('Toffoli', 3, 0, ToffoliClass)
#pq.ops.TimeEvolution #Gate for time evolution under a Hamiltonian (QubitOperator object).
AllZ = Gate('AllZ', 1, 0, AllZClass) #todo: 1 should be replaced by a way to specify "all"
# measurements
MeasureX = Observable('X', 1, 0, pq.ops.X)
MeasureY = Observable('Y', 1, 0, pq.ops.Y)
MeasureZ = Observable('Z', 1, 0, pq.ops.Z)
MeasureAllZ = Observable('AllZ', 1, 0, AllZClass) #todo: 1 should be replaced by a way to specify "all"
classical_demo = [
Command(X, [0], []),
Command(Swap, [0, 1], []),
]
demo = [
Command(Rx, [0], [ParRef(0)]),
Command(Rx, [1], [ParRef(1)]),
Command(CNOT, [0, 1], []),
]
# circuit templates
Contrary to other gates, ProjectQ does not have a class for the AllZ gate,
as it is implemented as a meta-gate.
For consistency we define this class, whose constructor is made to retun
a gate with the correct properties by overwriting __new__().
"""
def __new__(*par): # pylint: disable=no-method-argument
return pq.ops.Tensor(pq.ops.ZGate())
#gates
H = Gate('H', 1, 0, pq.ops.HGate)
X = Gate('X', 1, 0, pq.ops.XGate)
Y = Gate('Y', 1, 0, pq.ops.YGate)
Z = Gate('Z', 1, 0, pq.ops.ZGate)
S = Gate('S', 1, 0, pq.ops.SGate)
T = Gate('T', 1, 0, pq.ops.TGate)
SqrtX = Gate('SqrtX', 1, 0, pq.ops.SqrtXGate)
Swap = Gate('Swap', 2, 0, pq.ops.SwapGate)
SqrtSwap = Gate('SqrtSwap', 2, 0, pq.ops.SqrtSwapGate)
#Entangle = Gate('Entangle', n, 0, pq.ops.EntangleGate) #This gate acts on all qubits
#Ph = Gate('Ph', 0, 1, pq.ops.Ph) #This gate acts on all qubits or non, depending on how one looks at it...
Rx = Gate('Rx', 1, 1, pq.ops.Rx) #(angle) RotationX gate class
Ry = Gate('Ry', 1, 1, pq.ops.Ry) #(angle) RotationY gate class
Rz = Gate('Rz', 1, 1, pq.ops.Rz) #(angle) RotationZ gate class
R = Gate('R', 1, 1, pq.ops.R) #(angle) Phase-shift gate (equivalent to Rz up to a global phase)
#pq.ops.AllGate , which is the same as pq.ops.Tensor, is a meta gate that acts on all qubits
#pq.ops.QFTGate #This gate acts on all qubits
#pq.ops.QubitOperator #A sum of terms acting on qubits, e.g., 0.5 * ‘X0 X5’ + 0.3 * ‘Z1 Z2’
CRz = Gate('CRz', 2, 1, pq.ops.CRz) #(angle) Shortcut for C(Rz(angle), n=1).
CNOT = Gate('CNOT', 2, 0, CNOTClass)
CZ = Gate('CZ', 2, 0, CZClass)