Skip to content

Commit

Permalink
Merge branch 'main' into issue_51_replace_qiskit_ibm_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alex124585 authored May 10, 2024
2 parents 7631531 + 9cb8259 commit de148ef
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 41 deletions.
17 changes: 2 additions & 15 deletions .github/workflows/qokit-python-only.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ jobs:

steps:
- uses: actions/checkout@v3
###### Alex: to be used in future version once permissions issue is resolved
# - name: black
# uses: lgeiger/black-action@master
# with:
# args: .
# - name: Apply black formatting if reviewdog found formatting errors
# if: "${{ failure() }}"
# run: |
# black .
# - name: Commit black formatting results
# if: "${{ failure() }}"
# uses: stefanzweifel/git-auto-commit-action@v4
# with:
# commit_message: ":art: Format Python code with psf/black push"
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
Expand All @@ -47,7 +33,8 @@ jobs:
black --check .
- name: pip install
run: |
QOKIT_PYTHON_ONLY=1 pip install .
export QOKIT_PYTHON_ONLY=True
pip install .
- name: Run tests and coverage
run: |
pytest --cov=qokit -rs tests --cov-fail-under=70
Expand Down
81 changes: 57 additions & 24 deletions examples/QAOA_portfolio_optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "c3de09e7-5315-4170-8263-6041a7819fac",
"metadata": {
"tags": []
Expand All @@ -58,7 +58,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "57544f68-6538-4dda-a47b-254252caa54a",
"metadata": {
"tags": []
Expand All @@ -67,12 +67,12 @@
"source": [
"po_problem = get_problem(N=6,K=3,q=0.5,seed=1,pre=1)\n",
"means_in_spins = np.array([po_problem['means'][i] - po_problem['q'] * np.sum(po_problem['cov'][i, :]) for i in range(len(po_problem['means']))])\n",
"scale = 1 / (np.sqrt(np.mean((( po_problem['q']*po_problem['cov'])**2).flatten()))+np.sqrt(np.mean((means_in_spins**2).flatten())))"
"scale = 1 / np.sqrt(np.mean((( po_problem['q']*po_problem['cov'])**2).flatten())+np.mean((means_in_spins**2).flatten()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "e49f9152-e49c-4cbd-a2b3-20894a927a9a",
"metadata": {
"tags": []
Expand All @@ -84,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "a800d2c6-d7a9-449a-befc-e1620676847e",
"metadata": {
"tags": []
Expand All @@ -96,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "60942736-20e4-4519-a584-65f613b7bddf",
"metadata": {
"tags": []
Expand All @@ -118,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "c4cb1e0a-f0eb-48e4-a1dc-8d342462c156",
"metadata": {
"tags": []
Expand All @@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "1fa46296-89cb-40e7-aaf8-09b708eda12d",
"metadata": {
"tags": []
Expand All @@ -145,7 +145,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "4bb70cb8-db79-45b3-b808-96b7d6647b68",
"metadata": {
"tags": []
Expand All @@ -158,12 +158,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "238479eb-db75-4e19-a159-36a2bed9c2ae",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"energy = 0.004782805426149568, Approximation ratio = 0.767014699611637\n"
]
}
],
"source": [
"po_energy = qaoa_obj(x0).real\n",
"po_ar = (po_energy-best_portfolio[1])/(best_portfolio[0]-best_portfolio[1])\n",
Expand All @@ -184,7 +192,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "9d730369-3d9a-4e0e-bd2b-a81d92baa7b7",
"metadata": {
"tags": []
Expand All @@ -197,7 +205,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "55a761e4-2122-4b00-bc1f-39730d6a3dd7",
"metadata": {
"tags": []
Expand All @@ -224,12 +232,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "86b7ff7f-43f4-4242-95b9-82784f0632a5",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"energy = 0.004135567715727546, Approximation ratio = 0.8050399835274252\n"
]
}
],
"source": [
"_, opt_energy = minimize_nlopt(qaoa_obj, x0, p=1, rhobeg=0.01/1)\n",
"opt_ar = (opt_energy-best_portfolio[1])/(best_portfolio[0]-best_portfolio[1])\n",
Expand All @@ -238,12 +254,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "90c5c6af-003c-4724-a943-c747bd24f3c6",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"energy = 0.004135567763346608, Approximation ratio = 0.8050399807298003\n"
]
}
],
"source": [
"res = minimize(qaoa_obj, x0, method='COBYLA', options={'rhobeg':0.001})\n",
"print(f\"energy = {res.fun}, Approximation ratio = {(res.fun-best_portfolio[1])/(best_portfolio[0]-best_portfolio[1])}\")"
Expand All @@ -259,7 +283,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "0c6e7780-0392-4a9f-b0a7-4b23c0af84dc",
"metadata": {
"tags": []
Expand All @@ -271,7 +295,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "d5fec9f4-09e0-409f-a613-07f23194c94d",
"metadata": {
"tags": []
Expand Down Expand Up @@ -300,7 +324,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "bc26bf47-72ce-4944-9a9f-d217e4c77894",
"metadata": {
"tags": []
Expand All @@ -314,7 +338,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "0c4f35dc-9402-48ce-89ef-c5a80883aaf8",
"metadata": {
"tags": []
Expand All @@ -329,12 +353,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "8581ac1f-2675-4ccb-b311-ee7e658b2782",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_25329/3897112670.py:2: DeprecationWarning: The method ``qiskit.circuit.quantumcircuit.QuantumCircuit.bind_parameters()`` is deprecated as of qiskit 0.45.0. It will be removed no earlier than 3 months after the release date. Use assign_parameters() instead\n",
" qc2 = parameterized_qc.bind_parameters(np.hstack([x0[p:]/2, x0[:p]/2]))\n"
]
}
],
"source": [
"parameterized_qc = get_parameterized_qaoa_circuit(po_problem, depth=p)\n",
"qc2 = parameterized_qc.bind_parameters(np.hstack([x0[p:]/2, x0[:p]/2]))\n",
Expand All @@ -345,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "2ceed19a-9391-4eed-95b9-dbfc3b8d6a9d",
"metadata": {
"tags": []
Expand Down
4 changes: 2 additions & 2 deletions qokit/portfolio_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ def get_problem(N, K, q, seed=1, pre=False) -> dict[str, Any]:
po_problem["pre"] = pre
if pre == "rule":
means_in_spins = np.array([po_problem["means"][i] - q * np.sum(po_problem["cov"][i, :]) for i in range(len(po_problem["means"]))])
scale = 1 / (np.sqrt(np.mean(((q * po_problem["cov"]) ** 2).flatten())) + np.sqrt(np.mean((means_in_spins**2).flatten())))
# scale = 1 / (0.5*(np.sqrt(np.mean((po_problem['cov']**2).flatten()))+np.sqrt(np.mean((po_problem['means']**2).flatten()))))
scale = 1 / np.sqrt(np.mean(((q * po_problem["cov"]) ** 2).flatten()) + np.mean((means_in_spins**2).flatten()))
# scale = 1 / (0.5*(np.sqrt(np.mean((po_problem['cov']**2).flatten())+np.mean((po_problem['means']**2).flatten())))
elif np.isscalar(pre):
scale = pre
else:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_qaoa_objective_maxcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,29 @@ def test_sk_ini_maxcut(simulator):
else:
assert cur_ar > last_ar
last_ar = cur_ar


@pytest.mark.parametrize("simulator", simulators_to_run_names_no_qiskit)
def test_overlap_maxcut(simulator):
N = 4
d = 3
seed = 1
G = nx.random_regular_graph(d, N, seed=seed)
p = 1
beta = [np.random.uniform(0, 1)]
gamma = [np.random.uniform(0, 1)]

obj = partial(maxcut_obj, w=get_adjacency_matrix(G))
precomputed_energies = precompute_energies(obj, N)

f1 = get_qaoa_maxcut_objective(N, p, precomputed_cuts=precomputed_energies, parameterization="gamma beta", objective="overlap")
f2 = get_qaoa_maxcut_objective(N, p, G=G, parameterization="gamma beta", objective="overlap")

assert np.isclose(f1(gamma, beta), f2(gamma, beta))
assert np.isclose(f1([0], [0]), f2([0], [0]))

maxval = precomputed_energies.max()
bitstring_loc = (precomputed_energies == maxval).nonzero()
assert len(bitstring_loc) == 1
bitstring_loc = bitstring_loc[0]
assert np.isclose(1 - f1([0], [0]), len(bitstring_loc) / len(precomputed_energies))

0 comments on commit de148ef

Please sign in to comment.