mirror of https://github.com/Qiskit/qiskit.git
cleaning up optimization
This commit is contained in:
parent
388cd1b83a
commit
6c88dca5b3
|
@ -58,8 +58,9 @@
|
|||
"* [Superposition and entanglement](scripts/superposition_and_entanglement.ipynb) - illustrates how to make simple quantum states on one and two qubits and shows concepts such as a quantum superpositions and entanglement. \n",
|
||||
"* [Single qubit states: amplitude and phase](scripts/single_qubit_states_amplitude_and_phase.ipynb) TODO WITH JAY\n",
|
||||
"* [Entanglement revisited](scripts/entanglement_revisited.ipynb) - illustrates the CHSH inequality and extensions for three qubits (Mermin). \n",
|
||||
"* quantum teleportation TODO WITH JAY\n",
|
||||
"* quantum superdense coding TODO WITH JAY"
|
||||
"* Quantum sphere TODO WITH JAY\n",
|
||||
"* Quantum teleportation TODO WITH JAY\n",
|
||||
"* Quantum superdense coding TODO WITH JAY"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -91,7 +92,8 @@
|
|||
"The next set of notebooks show how you explore applications of short depth quantum computers. Currently we have:\n",
|
||||
"\n",
|
||||
"* [Quantum optimization](scripts/classical_optimization.ipynb) - illustrates how to use a quantum computer to look at optimization problems. \n",
|
||||
"* Quantum chemistry TODO WITH JAY"
|
||||
"* Quantum chemistry by VQE TODO WITH JAY\n",
|
||||
"* Iterative Phase estimation TODO WITH JAY"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -29,7 +29,7 @@
|
|||
"## Introduction\n",
|
||||
"In [superposition and entanglement](scripts/superposition_and_entanglement.ipynb) we looked at the concept of computational states, and superpostion states. Here we continue the developement of the understanding of a quantum state by introducing amplitude and phase. \n",
|
||||
"\n",
|
||||
"Simple put an arbitrary quantum state for a single qubit can be written as $|\\psi\\rangle = \\alpha |0\\rangle + \\beta |1\\rangle$ where $\\alpha$ and $\\beta$ are complex numbers with the only constratint that $|\\alpha|^2+ |\\beta|^2 =1$."
|
||||
"Simple put an arbitrary quantum state for a single qubit can be written as $|\\psi\\rangle = \\alpha |0\\rangle + \\beta |1\\rangle$ where $\\alpha$ and $\\beta$ are complex numbers with the only constratint that $$|\\alpha|^2+ |\\beta|^2 =1$$ "
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -37,7 +37,8 @@
|
|||
"metadata": {},
|
||||
"source": [
|
||||
"## Ampitude \n",
|
||||
"Please see the getting_started_with_the_qx_api if you would like to understand the API better. However for this script simply import the following preamble once you have made your own Qconfig.py file containing your token from Quantum Experience. "
|
||||
"\n",
|
||||
"Assuming for now that $\\alpha$ and $\\beta$ are real then the above implies that $|\\psi\\rangle = \\cos(\\theta) |0\\rangle + \\sin(\\theta) |1\\rangle$"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -884,7 +884,7 @@
|
|||
"metadata": {
|
||||
"anaconda-cloud": {},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "Python [default]",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
@ -898,7 +898,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.0"
|
||||
"version": "3.5.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
// Quantum Experience (QE) Standard Header
|
||||
// file: qelib1.inc
|
||||
|
||||
// --- QE Hardware primitives ---
|
||||
|
||||
// 3-parameter 2-pulse single qubit gate
|
||||
gate u3(theta,phi,lambda) q { U(theta,phi,lambda) q; }
|
||||
// 2-parameter 1-pulse single qubit gate
|
||||
gate u2(phi,lambda) q { U(pi/2,phi,lambda) q; }
|
||||
// 1-parameter 0-pulse single qubit gate
|
||||
gate u1(lambda) q { U(0,0,lambda) q; }
|
||||
// controlled-NOT
|
||||
gate cx c,t { CX c,t; }
|
||||
// idle gate (identity)
|
||||
gate id a { U(0,0,0) a; }
|
||||
|
||||
// --- QE Standard Gates ---
|
||||
|
||||
// Pauli gate: bit-flip
|
||||
gate x a { u3(pi,0,pi) a; }
|
||||
// Pauli gate: bit and phase flip
|
||||
gate y a { u3(pi,pi/2,pi/2) a; }
|
||||
// Pauli gate: phase flip
|
||||
gate z a { u1(pi) a; }
|
||||
// Clifford gate: Hadamard
|
||||
gate h a { u2(0,pi) a; }
|
||||
// Clifford gate: sqrt(Z) phase gate
|
||||
gate s a { u1(pi/2) a; }
|
||||
// Clifford gate: conjugate of sqrt(Z)
|
||||
gate sdg a { u1(-pi/2) a; }
|
||||
// C3 gate: sqrt(S) phase gate
|
||||
gate t a { u1(pi/4) a; }
|
||||
// C3 gate: conjugate of sqrt(S)
|
||||
gate tdg a { u1(-pi/4) a; }
|
||||
|
||||
// --- QE Standard User-Defined Gates ---
|
||||
|
||||
// controlled-Phase
|
||||
gate cz a,b { h b; cx a,b; h b; }
|
||||
// controlled-Y
|
||||
gate cy a,b { sdg b; cx a,b; s b; }
|
||||
// C3 gate: Toffoli
|
||||
gate ccx a,b,c
|
||||
{
|
||||
h c;
|
||||
cx b,c; tdg c;
|
||||
cx a,c; t c;
|
||||
cx b,c; tdg c;
|
||||
cx a,c; t b; t c; h c;
|
||||
cx a,b; t a; tdg b;
|
||||
cx a,b;
|
||||
}
|
||||
// controlled phase rotation
|
||||
gate cu1(lambda) a,b
|
||||
{
|
||||
u1(lambda/2) a;
|
||||
cx a,b;
|
||||
u1(-lambda/2) b;
|
||||
cx a,b;
|
||||
u1(lambda/2) b;
|
||||
}
|
||||
// controlled-U
|
||||
gate cu3(theta,phi,lambda) c, t
|
||||
{
|
||||
// implements controlled-U(theta,phi,lambda) with target t and control c
|
||||
u1((lambda-phi)/2) t;
|
||||
cx c,t;
|
||||
u3(-theta/2,0,-(phi+lambda)/2) t;
|
||||
cx c,t;
|
||||
u3(theta/2,phi,0) t;
|
||||
}
|
|
@ -5,7 +5,10 @@ These are simple methods for common tasks in our optimization,
|
|||
|
||||
Author: Jay Gambetta
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.path.append("..")
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
from qiskit.extensions.standard import h, u3, barrier, cz
|
||||
|
||||
def cost_classical(data, n, alpha, beta):
|
||||
"""Compute the cost function.
|
||||
|
@ -13,6 +16,8 @@ def cost_classical(data, n, alpha, beta):
|
|||
n = number of qubits
|
||||
alpha is a vector with elements q0 -- qn
|
||||
beta is a matrix of couplings
|
||||
|
||||
NOTE THIS SHOULD BE MADE TO WORK WITH UPPER TRIANGLE
|
||||
"""
|
||||
temp = 0
|
||||
tot = sum(data.values())
|
||||
|
@ -43,3 +48,32 @@ def cost_classical(data, n, alpha, beta):
|
|||
observable = observable - beta[n-1-i, n-1-j]
|
||||
temp += data[key]*observable/tot
|
||||
return temp
|
||||
|
||||
|
||||
def trial_funtion_optimization(n, m, theta, entangler_map):
|
||||
"""Trial function for classical optimization problems.
|
||||
|
||||
n = number of qubits
|
||||
m = depth
|
||||
theta = control vector of size n*m stacked as theta[n*i+j] where j counts
|
||||
the qubits and i the depth
|
||||
entangler_map = {0: [2, 1],
|
||||
1: [2],
|
||||
3: [2],
|
||||
4: [2]} control is the key and values are the target
|
||||
"""
|
||||
q = QuantumRegister("q", n)
|
||||
c = ClassicalRegister("c", n)
|
||||
trial_circuit = QuantumCircuit(q, c)
|
||||
trial_circuit.h(q)
|
||||
for i in range(m):
|
||||
trial_circuit.barrier(q)
|
||||
for node in entangler_map:
|
||||
for j in entangler_map[node]:
|
||||
trial_circuit.cz(q[node], q[j])
|
||||
for j in range(n):
|
||||
trial_circuit.u3(theta[n*i+j], 0, 0, q[j])
|
||||
trial_circuit.barrier(q)
|
||||
for j in range(n):
|
||||
trial_circuit.measure(q[j], c[j])
|
||||
return trial_circuit
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
"""Test the cost function for different basis functions."""
|
||||
import numpy as np
|
||||
from quantum_optimization import cost_classical
|
||||
|
||||
# cost function
|
||||
alpha = np.zeros(5)
|
||||
alpha[4] = 1
|
||||
beta = np.zeros((5, 5))
|
||||
beta[0, 1] = 0.5
|
||||
beta[1, 0] = 0.5
|
||||
beta[0, 2] = 0.5
|
||||
beta[2, 0] = 0.5
|
||||
beta[1, 2] = 0.5
|
||||
beta[2, 1] = 0.5
|
||||
beta[1, 3] = 0.5
|
||||
beta[3, 1] = 0.5
|
||||
beta[2, 3] = 0.5
|
||||
beta[3, 2] = 0.5
|
||||
|
||||
n = 5
|
||||
data = {'00000': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00001': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00010': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00011': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00100': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00101': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00110': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'00111': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01000': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01001': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01010': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01011': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01100': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01101': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01110': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'01111': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10000': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10001': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10010': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10011': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10100': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10101': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10110': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'10111': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11000': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11001': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11010': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11011': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11100': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11101': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11110': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
||||
data = {'11111': 10}
|
||||
print(cost_classical(data, n, alpha, beta))
|
|
@ -0,0 +1,16 @@
|
|||
"""Test the cost function for different basis functions."""
|
||||
import sys
|
||||
sys.path.append("..")
|
||||
import numpy as np
|
||||
from quantum_optimization import trial_funtion_optimization
|
||||
from scripts.qhelpers.misc import program_to_text
|
||||
|
||||
entangler_map = {0: [2], 1: [2], 3: [2], 4: [2]}
|
||||
|
||||
m = 1
|
||||
n = 6
|
||||
theta = np.zeros(m*n)
|
||||
trial_circuit = trial_funtion_optimization(n, m, theta, entangler_map)
|
||||
|
||||
program = [trial_circuit]
|
||||
print(program_to_text(program))
|
Loading…
Reference in New Issue