This file contains hidden or 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
f, ax = plt.subplots(figsize=(20, 8)) | |
optimized_value = sns.barplot(state, probabilities) | |
for item in optimized_value.get_xticklabels(): | |
item.set_rotation(45) | |
plt.title("Probabilites of Each Possible Combination of Assets", fontsize=25) # Probability of Max Return at given risk level | |
plt.xlabel('Possible Combinations of Assets',fontsize =20) | |
plt.ylabel('Probability',fontsize = 20) | |
plt.show() |
This file contains hidden or 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
%time | |
state, values, probabilities = qaoa(qubitOp) # ['FB.O', 'AAPL.O', 'AMZN.O', 'NFLX.O', 'GOOGL.O'] |
This file contains hidden or 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
def qaoa(qubitOp): | |
backend = Aer.get_backend('statevector_simulator') # You can switch to different backends by providing the name of backend. | |
seed = 50 | |
cobyla = COBYLA() | |
cobyla.set_options(maxiter=1000) | |
qaoa = QAOA(qubitOp, cobyla, 3) | |
qaoa.random_seed = seed | |
quantum_instance = QuantumInstance(backend=backend, seed_simulator=seed, seed_transpiler=seed) | |
results= qaoa.run(quantum_instance) | |
selection, state, values, probabilities = print_result(results) |
This file contains hidden or 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
f, ax = plt.subplots(figsize=(20, 8)) | |
optimized_value = sns.barplot(state, probabilities) | |
for item in optimized_value.get_xticklabels(): | |
item.set_rotation(45) | |
plt.title("Probabilites of Each Possible Combination of Assets", fontsize=25) # Probability of Max Return at given risk level | |
plt.xlabel('Possible Combinations of Assets',fontsize =20) | |
plt.ylabel('Probability',fontsize = 20) | |
plt.show() |
This file contains hidden or 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
%time | |
state, values, probabilities = vqe(qubitOp) # ['FB.O', 'AAPL.O', 'AMZN.O', 'NFLX.O', 'GOOGL.O'] |
This file contains hidden or 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
def vqe(qubitOp): | |
backend = Aer.get_backend('statevector_simulator') # You can switch to different backends by providing the name of backend. | |
seed = 50 | |
cobyla = COBYLA() | |
cobyla.set_options(maxiter=1000) | |
ry = TwoLocal(qubitOp.num_qubits, 'ry', 'cz', reps=3, entanglement='full') | |
vqe = VQE(qubitOp, ry, cobyla) | |
vqe.random_seed = seed | |
quantum_instance = QuantumInstance(backend=backend, seed_simulator=seed, seed_transpiler=seed) | |
result = vqe.run(quantum_instance) |
This file contains hidden or 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
f, ax = plt.subplots(figsize=(20, 8)) | |
optimized_value = sns.barplot(state, probabilities) | |
for item in optimized_value.get_xticklabels(): | |
item.set_rotation(45) | |
plt.title("Probabilites of Each Possible Combination of Assets", fontsize=25) # Probability of Max Return at given risk level | |
plt.xlabel('Possible Combinations of Assets',fontsize =20) | |
plt.ylabel('Probability',fontsize = 20) | |
plt.show() |
This file contains hidden or 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
%time | |
state, values, probabilities = numpyEigensolver(qubitOp) # ['FB.O', 'AAPL.O', 'AMZN.O', 'NFLX.O', 'GOOGL.O'] |
This file contains hidden or 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
def numpyEigensolver(qubitOp): | |
selections = [] | |
exact_eigensolver = NumPyMinimumEigensolver(qubitOp) | |
result = exact_eigensolver.run() | |
selection, state, values, probabilities = print_result(result) | |
print(selection_to_picks(num_assets, selection)) | |
return state, values, probabilities |
This file contains hidden or 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
def print_result(result): | |
selection = sample_most_likely(result.eigenstate) | |
value = portfolio.portfolio_value(selection, mu, sigma, risk_factor, budget, penalty) | |
print('Optimal: selection {}, value {:.4f}'.format(selection, value)) | |
eigenvector = result.eigenstate if isinstance(result.eigenstate, np.ndarray) else result.eigenstate.to_matrix() | |
probabilities = np.abs(eigenvector)**2 | |
i_sorted = reversed(np.argsort(probabilities)) | |
print('\n----------------- Full result ---------------------') | |
print('selection\tvalue\t\tprobability') | |
print('---------------------------------------------------') |
NewerOlder