Last active
August 12, 2020 17:03
-
-
Save NimishMishra/05dcc69edff3448f3828aba3ceb7221d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from qiskit.aqua.operators import Z, Y, X | |
from qiskit.aqua.operators import StateFn | |
QUBITS = 4 | |
operatorZ = Z ^ Z ^ Z ^ Z | |
operatorX = X ^ X ^ X ^ X | |
operatorY = Y ^ Y ^ Y ^ Y | |
def quantum_layer(initial_parameters): | |
# expecting parameters to be a numpy array | |
quantumRegister = QuantumRegister(QUBITS) | |
quantumCircuit = QuantumCircuit(quantumRegister) | |
quantumCircuit.h(range(4)) | |
for i in range(len(initial_parameters)): | |
quantumCircuit.ry(initial_parameters[i] * np.pi, i) | |
psi = StateFn(quantumCircuit) | |
# two ways of doing the same thing | |
expectationX = (~psi @ operatorX @ psi).eval() | |
expectationZ = psi.adjoint().compose(operatorZ).compose(psi).eval().real | |
expectationY = (~psi @ operatorY @ psi).eval() | |
expectationZ = np.abs(np.real(expectationZ)) | |
expectations = [expectationX, expectationY, expectationZ, | |
expectationX + expectationY + expectationZ] | |
return np.array(expectations) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment