-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
286 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from time import time" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 53, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def print_stats(circuit):\n", | ||
" print('Depth:', circuit.depth())\n", | ||
" print('Num qubits:', circuit.num_qubits)\n", | ||
" print('Ops:', circuit.count_ops())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# UCCSD" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 54, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from qiskit.circuit import ParameterVector\n", | ||
"from qiskit.chemistry.components.variational_forms import UCCSD\n", | ||
"from qiskit.aqua.operators import Z2Symmetries\n", | ||
"from qiskit.chemistry.drivers import PySCFDriver, UnitsType\n", | ||
"from qiskit.chemistry import FermionicOperator" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 55, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_qubit_op(dist, molecule=\"LiH\", basis=\"sto3g\"):\n", | ||
" if molecule == \"LiH\":\n", | ||
" driver = PySCFDriver(atom=\"Li .0 .0 .0; H .0 .0 \" + str(dist), unit=UnitsType.ANGSTROM, \n", | ||
" charge=0, spin=0, basis=basis)\n", | ||
" elif molecule == \"BeH2\":\n", | ||
" driver = PySCFDriver(atom=\"Be .0 .0 .0; H .0 .0 -\" + str(dist) + \"; H .0 .0 \" + str(dist), \n", | ||
" unit=UnitsType.ANGSTROM, \n", | ||
" charge=0, spin=0, basis=basis)\n", | ||
" molecule = driver.run()\n", | ||
" freeze_list = [0]\n", | ||
" remove_list = [-3, -2]\n", | ||
" repulsion_energy = molecule.nuclear_repulsion_energy\n", | ||
" num_particles = molecule.num_alpha + molecule.num_beta\n", | ||
" num_spin_orbitals = molecule.num_orbitals * 2\n", | ||
" remove_list = [x % molecule.num_orbitals for x in remove_list]\n", | ||
" freeze_list = [x % molecule.num_orbitals for x in freeze_list]\n", | ||
" remove_list = [x - len(freeze_list) for x in remove_list]\n", | ||
" remove_list += [x + molecule.num_orbitals - len(freeze_list) for x in remove_list]\n", | ||
" freeze_list += [x + molecule.num_orbitals for x in freeze_list]\n", | ||
" ferOp = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals)\n", | ||
" ferOp, energy_shift = ferOp.fermion_mode_freezing(freeze_list)\n", | ||
" num_spin_orbitals -= len(freeze_list)\n", | ||
" num_particles -= len(freeze_list)\n", | ||
" ferOp = ferOp.fermion_mode_elimination(remove_list)\n", | ||
" num_spin_orbitals -= len(remove_list)\n", | ||
" qubitOp = ferOp.mapping(map_type='parity', threshold=0.00000001)\n", | ||
" qubitOp = Z2Symmetries.two_qubit_reduction(qubitOp, num_particles)\n", | ||
" shift = energy_shift + repulsion_energy\n", | ||
" return qubitOp, num_particles, num_spin_orbitals, shift\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Try `LiH` or `BeH2` as molecules, `sto-3g` for a small basis set and `3-21g` as large basis set.\n", | ||
"To make the circuit deeper, you can set `reps` to a value larger than 1." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Constructed in 1.8730289936065674s\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"molecule = 'LiH'\n", | ||
"basis_set = '3-21g'\n", | ||
"reps = 1\n", | ||
"dist = 1.3\n", | ||
"qubitOp, num_particles, num_spin_orbitals, shift = get_qubit_op(dist, molecule, basis_set)\n", | ||
"var_form = UCCSD(\n", | ||
" num_orbitals=num_spin_orbitals,\n", | ||
" num_particles=num_particles,\n", | ||
" reps=1,\n", | ||
" qubit_mapping='parity'\n", | ||
")\n", | ||
"t0 = time()\n", | ||
"circuit = var_form.construct_circuit(ParameterVector('P', length=var_form.num_parameters))\n", | ||
"print('Constructed in {}s'.format(time() - t0))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 52, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Depth: 2406\n", | ||
"Num qubits: 10\n", | ||
"Ops: OrderedDict([('u3', 1960), ('cx', 1312), ('cmp_dg', 14), ('cmp', 8), ('cLinRot', 8), ('cmp_dg_dg', 6), ('cLinRot_dg', 6), ('LinRot', 4), ('LinRot_dg', 3), ('h', 2), ('cu1', 1)])\n", | ||
"Ops (decomposed): OrderedDict([('u3', 2156), ('cx', 1510), ('x', 280), ('ccx', 168), ('cry', 21), ('ry', 7), ('u1', 3), ('u2', 2)])\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print_stats(circuit.decompose())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# QAE" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from qiskit.aqua.algorithms import AmplitudeEstimation\n", | ||
"from qiskit.circuit.library import EfficientSU2\n", | ||
"from qiskit.aqua.components.uncertainty_models import NormalDistribution\n", | ||
"from qiskit.aqua.components.uncertainty_problems import UnivariatePiecewiseLinearObjective" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 43, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class AFactory(CircuitFactory):\n", | ||
" def __init__(self, objective, distribution):\n", | ||
" super().__init__(distribution.num_target_qubits + 1)\n", | ||
" self.objective = objective\n", | ||
" self.distribution = distribution\n", | ||
" self.i_state = list(range(distribution.num_target_qubits))\n", | ||
" self.i_target = distribution.num_target_qubits\n", | ||
" \n", | ||
" def required_ancillas(self):\n", | ||
" return self.objective.required_ancillas()\n", | ||
" \n", | ||
" def required_ancillas_controlled(self):\n", | ||
" return self.objective.required_ancillas_controlled()\n", | ||
" \n", | ||
" def build(self, qc, q, q_ancillas=None, params=None):\n", | ||
" qr_state = [q[i] for i in self.i_state]\n", | ||
" qr_target = [q[self.i_target]]\n", | ||
" \n", | ||
" self.distribution.build(qc, qr_state)\n", | ||
" self.objective.build(qc, q, q_ancillas=q_ancillas)\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 62, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"/Users/jul/Work/Qiskit/qiskit-aqua/qiskit/aqua/components/uncertainty_problems/univariate_piecewise_linear_objective.py:164: DeprecationWarning: The qiskit.aqua.circuits.PiecewiseLinearRotation object is deprecated and will be removed no earlier than 3 months after the 0.7.0 release of Qiskit Aqua. You should use qiskit.circuit.library.PiecewiseLinearPauliRotations instead.\n", | ||
" # create piecewise linear Y rotation\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"num_state_qubits = 4\n", | ||
"breakpoints = [0, 2, 5]\n", | ||
"slopes = [1, 2, 5]\n", | ||
"offsets = [-1, 0, 1]\n", | ||
"f_min = 0\n", | ||
"f_max = 12\n", | ||
"c_approx = 0.1\n", | ||
"distribution = NormalDistribution(num_state_qubits)\n", | ||
"pwl = UnivariatePiecewiseLinearObjective(num_state_qubits, 0, 10, \n", | ||
" breakpoints, slopes, offsets,\n", | ||
" f_min, f_max, c_approx)\n", | ||
"\n", | ||
"a_factory = AFactory(pwl, distribution)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 63, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Constructed in 16.878931999206543s\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"m = 5\n", | ||
"qae = AmplitudeEstimation(m, a_factory=a_factory)\n", | ||
"t0 = time()\n", | ||
"circuit = qae.construct_circuit()\n", | ||
"print('Constructed in {}s'.format(time() - t0))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 64, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Depth: 47750\n", | ||
"Num qubits: 15\n", | ||
"Ops: OrderedDict([('u1', 31139), ('cx', 27200), ('u2', 5771), ('u3', 2309), ('pw_lin', 32), ('pw_lin_dg', 31), ('iqft', 1)])\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print_stats(circuit)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3.7.5 64-bit ('latest': virtualenv)", | ||
"language": "python", | ||
"name": "python37564bitlatestvirtualenv76cfe9d7d3f749489cf2ff69593234af" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |