Skip to content

Commit

Permalink
Updates to latest API
Browse files Browse the repository at this point in the history
  • Loading branch information
manoelmarques committed May 8, 2020
1 parent ae7da09 commit 566236e
Show file tree
Hide file tree
Showing 52 changed files with 1,110 additions and 1,358 deletions.
38 changes: 15 additions & 23 deletions aqua/algorithm_introduction_with_vqe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"\n",
"This notebook demonstrates how to use the `Qiskit Aqua` library to invoke an algorithm and process the result.\n",
"\n",
"Further information may be found for the algorithms in the online [Aqua documentation](https://qiskit.org/documentation/aqua/algorithms.html).\n",
"Further information may be found for the algorithms in the online [Aqua documentation](https://qiskit.org/documentation/apidoc/qiskit.aqua.algorithms.html).\n",
"\n",
"Algorithms in Aqua can be created and run as usual in Python by constructing instances and calling methods.\n",
"\n",
Expand Down Expand Up @@ -65,9 +65,9 @@
"source": [
"### Let's start with a classical algorithm\n",
"\n",
"We can now use the Operator without regard to how it was created. We chose to start this tutorial with a classical algorithm as it involves a little less setting up than the `VQE` quantum algorithm we will use later. Here we will use `ExactEigensolver` to compute the minimum eigenvalue of the Operator (Hamiltonian).\n",
"We can now use the Operator without regard to how it was created. We chose to start this tutorial with a classical algorithm as it involves a little less setting up than the `VQE` quantum algorithm we will use later. Here we will use `NumPyMinimumEigensolver` to compute the minimum eigenvalue of the Operator (Hamiltonian).\n",
"\n",
"We construct an `ExactEigensolver` instance, passing in the Operator, and then call `run()` on in order to compute the result. All Aqua algorithms have the run method (it is defined by a base class which all algorithms extend) and while no parameters are need for classical algorithms a quantum algorithm will require a backend (quantum simulator or real device) on which it will be run. The `result` object returned is a dictionary. While the results fields can be different for algorithms solving different problems, and even within a given problem type there may be algorithm specific data returned, for a given problem the fields core to that problem are common across algorithms in order that different algorithms can be chosen to solve the same problem in a consistent fashion."
"We construct an `NumPyMinimumEigensolver` instance, passing in the Operator, and then call `run()` on in order to compute the result. All Aqua algorithms have the run method (it is defined by a base class which all algorithms extend) and while no parameters are need for classical algorithms a quantum algorithm will require a backend (quantum simulator or real device) on which it will be run. The `result` object returned is a dictionary. While the results fields can be different for algorithms solving different problems, and even within a given problem type there may be algorithm specific data returned, for a given problem the fields core to that problem are common across algorithms in order that different algorithms can be chosen to solve the same problem in a consistent fashion."
]
},
{
Expand All @@ -84,11 +84,11 @@
}
],
"source": [
"from qiskit.aqua.algorithms import ExactEigensolver\n",
"from qiskit.aqua.algorithms import NumPyMinimumEigensolver\n",
"\n",
"ee = ExactEigensolver(qubit_op)\n",
"ee = NumPyMinimumEigensolver(qubit_op)\n",
"result = ee.run()\n",
"print(result['energy'])"
"print(result.eigenvalue.real)"
]
},
{
Expand Down Expand Up @@ -116,29 +116,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"-1.8572750302017973\n"
"-1.85727503014758\n"
]
}
],
"source": [
"from qiskit import BasicAer\n",
"from qiskit.aqua.algorithms import VQE\n",
"from qiskit.aqua.components.variational_forms import RYRZ\n",
"from qiskit.circuit.library import TwoLocal\n",
"from qiskit.aqua.components.optimizers import L_BFGS_B\n",
"\n",
"var_form = RYRZ(qubit_op.num_qubits, depth=3, entanglement='linear')\n",
"var_form = TwoLocal(qubit_op.num_qubits, ['ry', 'rz'], 'cz', reps=3, entanglement='linear')\n",
"optimizer = L_BFGS_B(maxfun=1000)\n",
"vqe = VQE(qubit_op, var_form, optimizer)\n",
"backend = BasicAer.get_backend('statevector_simulator')\n",
"result = vqe.run(backend)\n",
"print(result['energy'])"
"print(result.eigenvalue.real)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following shows the above but using a QuantumInstance and setting up a default transpiler PassManager for circuit processing."
"The following shows the above but using a QuantumInstance."
]
},
{
Expand All @@ -150,21 +150,20 @@
"name": "stdout",
"output_type": "stream",
"text": [
"-1.8572750301967933\n"
"-1.85727503011329\n"
]
}
],
"source": [
"from qiskit.aqua import QuantumInstance\n",
"from qiskit.transpiler import PassManager\n",
"\n",
"var_form = RYRZ(qubit_op.num_qubits, depth=3, entanglement='linear')\n",
"var_form = TwoLocal(qubit_op.num_qubits, ['ry', 'rz'], 'cz', reps=3, entanglement='linear')\n",
"optimizer = L_BFGS_B(maxfun=1000)\n",
"vqe = VQE(qubit_op, var_form, optimizer)\n",
"backend = BasicAer.get_backend('statevector_simulator')\n",
"qi = QuantumInstance(backend=backend, pass_manager=PassManager())\n",
"qi = QuantumInstance(backend=backend)\n",
"result = vqe.run(qi)\n",
"print(result['energy'])"
"print(result.eigenvalue.real)"
]
},
{
Expand All @@ -175,13 +174,6 @@
"\n",
"This completes an introduction to programming and using Aqua algorithms. There are plenty of other tutorials showing Aqua being used to solve other problems, including Machine Learning, Finance, Optimization and Chemistry. We encourage you to explore these further and see that various capabilities and techniques employed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
15 changes: 3 additions & 12 deletions aqua/eoh.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"\n",
"This notebook demonstrates how to use the `Qiskit Aqua` library to invoke the EOH algorithm and process the result.\n",
"\n",
"Further information may be found for the algorithms in the online [Aqua documentation](https://qiskit.org/documentation/aqua/algorithms.html).\n",
"Further information may be found for the algorithms in the online [Aqua documentation](https://qiskit.org/documentation/apidoc/qiskit.aqua.algorithms.html).\n",
"\n",
"For this particular demonstration, we illustrate the `EOH` algorithm. First, two `WeightedPauliOperator` instances we created are randomly generated Hamiltonians."
]
Expand All @@ -17,16 +17,7 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Converting time from a MatrixOperator to a Pauli-type Operator grows exponentially. If you are converting a system with large number of qubits, it will take time. You can turn on DEBUG logging to check the progress.\n",
"Converting time from a MatrixOperator to a Pauli-type Operator grows exponentially. If you are converting a system with large number of qubits, it will take time. You can turn on DEBUG logging to check the progress.\n"
]
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"from qiskit import BasicAer\n",
Expand Down Expand Up @@ -79,7 +70,7 @@
"output_type": "stream",
"text": [
"The result is\n",
"{'avg': (3.024786762103841-1.1976592430219064e-16j), 'std_dev': 0.0}\n"
"{'avg': (3.3315538738820436-1.1573250579094587e-16j), 'std_dev': 0.0}\n"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion aqua/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"\n",
"The following notebooks are noted:\n",
"\n",
"* [LiH plot using ExactEigensolver](../chemistry/energyplot.ipynb) One step up from getting started\n",
"* [LiH plot using NumPyMinimumEigensolver](../chemistry/energyplot.ipynb) One step up from getting started\n",
"* [H2 dissociation curve using VQE with UCCSD](../chemistry/h2_uccsd.ipynb)\n",
"* [LiH dissociation curve using VQE with UCCSD](../chemistry/lih_uccsd.ipynb)\n",
"* [NaH dissociation curve using VQE with UCCSD](../chemistry/nah_uccsd.ipynb)\n",
Expand Down
66 changes: 29 additions & 37 deletions aqua/simulations_with_noise_and_measurement_error_mitigation.ipynb

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions aqua/vqe2iqpe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"from qiskit import BasicAer\n",
"from qiskit.transpiler import PassManager\n",
"from qiskit.aqua import QuantumInstance\n",
"from qiskit.aqua.algorithms import VQE, ExactEigensolver, IQPE\n",
"from qiskit.aqua.algorithms import VQE, NumPyMinimumEigensolver, IQPE\n",
"from qiskit.aqua.operators import WeightedPauliOperator\n",
"from qiskit.aqua.components.variational_forms import RYRZ\n",
"from qiskit.circuit.library import TwoLocal\n",
"from qiskit.aqua.components.optimizers import SPSA\n",
"from qiskit.aqua.components.initial_states.var_form_based import VarFormBased"
]
Expand Down Expand Up @@ -57,7 +57,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now use the Operator without regard to how it was created. First we will use the ExactEigensolver to compute the reference ground energy level."
"We can now use the Operator without regard to how it was created. First we will use the NumPyMinimumEigensolver to compute the reference ground energy level."
]
},
{
Expand All @@ -74,8 +74,8 @@
}
],
"source": [
"result_reference = ExactEigensolver(qubit_op).run()\n",
"print('The reference ground energy level is {}.'.format(result_reference['energy']))"
"result_reference = NumPyMinimumEigensolver(qubit_op).run()\n",
"print('The reference ground energy level is {}.'.format(result_reference.eigenvalue.real))"
]
},
{
Expand All @@ -96,7 +96,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"VQE estimated the ground energy to be -1.8244194269951426.\n"
"VQE estimated the ground energy to be -1.7368866995757903.\n"
]
}
],
Expand All @@ -106,7 +106,7 @@
"backend = BasicAer.get_backend('qasm_simulator')\n",
"\n",
"var_form_depth = 3\n",
"var_form = RYRZ(qubit_op.num_qubits, var_form_depth)\n",
"var_form = TwoLocal(qubit_op.num_qubits, ['ry', 'rz'], 'cz', reps=var_form_depth)\n",
"\n",
"spsa_max_trials=10\n",
"optimizer = SPSA(max_trials=spsa_max_trials)\n",
Expand All @@ -115,7 +115,7 @@
"\n",
"quantum_instance = QuantumInstance(backend)\n",
"result_vqe = vqe.run(quantum_instance)\n",
"print('VQE estimated the ground energy to be {}.'.format(result_vqe['energy']))"
"print('VQE estimated the ground energy to be {}.'.format(result_vqe.eigenvalue.real))"
]
},
{
Expand All @@ -131,7 +131,7 @@
"metadata": {},
"outputs": [],
"source": [
"state_in = VarFormBased(var_form, result_vqe['opt_params'])"
"state_in = VarFormBased(var_form, result_vqe.optimal_point)"
]
},
{
Expand Down Expand Up @@ -164,7 +164,7 @@
"quantum_instance = QuantumInstance(backend, shots=100, seed_simulator=random_seed, seed_transpiler=random_seed)\n",
"result_iqpe = iqpe.run(quantum_instance)\n",
"print(\"Continuing with VQE's result, IQPE estimated the ground energy to be {}.\".format(\n",
" result_iqpe['energy']))"
" result_iqpe.eigenvalue.real))"
]
},
{
Expand Down
30 changes: 11 additions & 19 deletions aqua/vqe_convergence.ipynb

Large diffs are not rendered by default.

52 changes: 28 additions & 24 deletions chemistry/LiH_with_qubit_tapering_and_uccsd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Couldn't find cython int routine\n",
"Couldn't find cython int routine\n"
]
}
],
"source": [
"# import common packages\n",
"import itertools\n",
Expand All @@ -25,18 +34,15 @@
"\n",
"from qiskit import BasicAer\n",
"\n",
"from qiskit.aqua import set_qiskit_aqua_logging, QuantumInstance\n",
"from qiskit.aqua import QuantumInstance\n",
"from qiskit.aqua.operators import Z2Symmetries, WeightedPauliOperator\n",
"from qiskit.aqua.algorithms.adaptive import VQE\n",
"from qiskit.aqua.algorithms.classical import ExactEigensolver\n",
"from qiskit.aqua.algorithms import VQE, NumPyMinimumEigensolver\n",
"from qiskit.aqua.components.optimizers import COBYLA\n",
"\n",
"from qiskit.chemistry.drivers import PySCFDriver, UnitsType\n",
"from qiskit.chemistry.core import Hamiltonian, TransformationType, QubitMappingType \n",
"from qiskit.chemistry.components.variational_forms import UCCSD\n",
"from qiskit.chemistry.components.initial_states import HartreeFock\n",
"\n",
"# set_qiskit_aqua_logging(logging.INFO)"
"from qiskit.chemistry.components.initial_states import HartreeFock"
]
},
{
Expand All @@ -61,7 +67,7 @@
"output_type": "stream",
"text": [
"Originally requires 8 qubits\n",
"Representation: paulis, qubits: 8, size: 276\n"
"Electronic Hamiltonian: Representation: paulis, qubits: 8, size: 276\n"
]
}
],
Expand Down Expand Up @@ -180,10 +186,9 @@
}
],
"source": [
"ee = ExactEigensolver(qubit_op, k=1)\n",
"ee = NumPyMinimumEigensolver(qubit_op)\n",
"result = core.process_algorithm_result(ee.run())\n",
"for line in result[0]:\n",
" print(line)"
"print(result)"
]
},
{
Expand Down Expand Up @@ -214,8 +219,8 @@
"smallest_eig_value = 99999999999999\n",
"smallest_idx = -1\n",
"for idx in range(len(tapered_ops)):\n",
" ee = ExactEigensolver(tapered_ops[idx], k=1)\n",
" curr_value = ee.run()['energy']\n",
" ee = NumPyMinimumEigensolver(tapered_ops[idx])\n",
" curr_value = ee.run().eigenvalue.real\n",
" if curr_value < smallest_eig_value:\n",
" smallest_eig_value = curr_value\n",
" smallest_idx = idx\n",
Expand Down Expand Up @@ -284,24 +289,23 @@
"text": [
"=== GROUND STATE ENERGY ===\n",
" \n",
"* Electronic ground state energy (Hartree): -8.874303841496\n",
" - computed part: -1.078084272725\n",
"* Electronic ground state energy (Hartree): -8.874303856889\n",
" - computed part: -1.078084288118\n",
" - frozen energy part: -7.796219568771\n",
" - particle hole part: 0.0\n",
"~ Nuclear repulsion energy (Hartree): 0.992207270475\n",
"> Total ground state energy (Hartree): -7.882096571021\n",
"> Total ground state energy (Hartree): -7.882096586414\n",
"The parameters for UCCSD are:\n",
"[ 0.03803094 0.00360661 0.0382837 0.00369849 -0.03608325 0.05942172\n",
" -0.02729715 -0.02732059 0.05965191 -0.11498381]\n"
"[ 0.03815735 0.00366554 0.03827111 0.00369737 -0.03604811 0.0594364\n",
" -0.02741369 -0.02735108 0.05956488 -0.11497243]\n"
]
}
],
"source": [
"result = core.process_algorithm_result(algo_result)\n",
"for line in result[0]:\n",
" print(line)\n",
"print(result)\n",
"\n",
"print(\"The parameters for UCCSD are:\\n{}\".format(algo_result['opt_params']))"
"print(\"The parameters for UCCSD are:\\n{}\".format(algo_result.optimal_point))"
]
},
{
Expand All @@ -314,9 +318,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Quantum (Dev)",
"display_name": "Python 3",
"language": "python",
"name": "quantum-dev"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -328,7 +332,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 566236e

Please sign in to comment.