Skip to content

Instantly share code, notes, and snippets.

@NimishMishra
Last active August 12, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NimishMishra/05dcc69edff3448f3828aba3ceb7221d to your computer and use it in GitHub Desktop.
Save NimishMishra/05dcc69edff3448f3828aba3ceb7221d to your computer and use it in GitHub Desktop.
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