Skip to content

Commit 8687ce1

Browse files
committed
Added some unit tests
Fixed constructor for QAOA
1 parent cf44b77 commit 8687ce1

3 files changed

Lines changed: 132 additions & 27 deletions

File tree

qbraid_algorithms/qaoa/qaoa.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class QAOA:
66
def __init__(self, num_qubits : int, qasm_version : int = 3, use_input : bool = True):
77
self.builder = QasmBuilder(num_qubits, version=qasm_version)
88
self.use_input = use_input
9+
self._x_mixer_count = 0
10+
self._max_clique_cost_count = 0
11+
self._xy_mixer_count = 0
12+
self._min_vertex_cover_cost_count = 0
13+
self._maxcut_cost_count = 0
914

1015

1116
def xy_mixer(self, graph : nx.Graph) -> str:

qbraid_algorithms/qaoa/test.ipynb

Lines changed: 35 additions & 27 deletions
Large diffs are not rendered by default.

tests/test_qaoa.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright 2025 qBraid
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""
15+
Tests for QAOA implementation.
16+
"""
17+
import io
18+
import os
19+
import sys
20+
import tempfile
21+
from pathlib import Path
22+
23+
import pyqasm
24+
from pyqasm.modules.base import QasmModule
25+
26+
from qbraid_algorithms import qaoa
27+
from qbraid_algorithms.utils import get_max_count
28+
29+
import networkx as nx
30+
31+
from .local_device import LocalDevice
32+
33+
def test_generate_program():
34+
"""Test that generate_program correctly returns a str object."""
35+
qaoa_module = qaoa.QAOA(4)
36+
edges = [(0, 1), (0, 2), (0, 4), (1, 2), (2, 3), (3, 4)]
37+
graph = nx.Graph(edges)
38+
qaoa_module.setup_maxcut(graph=graph)
39+
program = qaoa_module.generate_algorithm(2)
40+
assert isinstance(program, str)
41+
assert qaoa_module.builder.qubits == 4 # 4 data qubits
42+
43+
44+
def test_unroll():
45+
"""Test that pyqasm unrolls correclty."""
46+
qaoa_module = qaoa.QAOA(4)
47+
edges = [(0, 1), (0, 2), (0, 4), (1, 2), (2, 3), (3, 4)]
48+
graph = nx.Graph(edges)
49+
qaoa_module.setup_maxcut(graph=graph)
50+
program = qaoa_module.generate_algorithm(2)
51+
module = pyqasm.loads(program)
52+
module.unroll()
53+
54+
def test_correct_hamiltonian_from_graph():
55+
"""Test that the cost Hamiltonian for maxcut is generated correctly."""
56+
qaoa_module = qaoa.QAOA(4)
57+
edges = [(0, 1), (0, 2)]
58+
graph = nx.Graph(edges)
59+
qaoa_module.setup_maxcut(graph=graph)
60+
program = qaoa_module.generate_algorithm(2)
61+
assert ("cnot qubits[0],qubits[1];"+os.linesep+
62+
"rz(-2 * gamma) qubits[1];"+os.linesep+
63+
"cnot qubits[0],qubits[1];"+os.linesep+
64+
"cnot qubits[0],qubits[2];"+os.linesep+
65+
"rz(-2 * gamma) qubits[2];"+os.linesep+
66+
"cnot qubits[0],qubits[2];") in program
67+
68+
def test_use_input():
69+
"""Test the use_input parameter."""
70+
qaoa_module = qaoa.QAOA(4, use_input=False)
71+
edges = [(0, 1), (0, 2)]
72+
graph = nx.Graph(edges)
73+
qaoa_module.setup_maxcut(graph=graph)
74+
program = qaoa_module.generate_algorithm(2, [1, 2, 3, 4])
75+
assert "gamma_0 = 1" in program
76+
assert "alpha_0 = 2" in program
77+
assert "gamma_1 = 3" in program
78+
assert "alpha_1 = 4" in program
79+
80+
81+
def test_execution():
82+
"""Test correct execution in local device."""
83+
device = LocalDevice()
84+
qaoa_module = qaoa.QAOA(4, use_input=False)
85+
edges = [(0, 1), (0, 2)]
86+
graph = nx.Graph(edges)
87+
qaoa_module.setup_maxcut(graph=graph)
88+
program = qaoa_module.generate_algorithm(2, [1, 2, 3, 4])
89+
module = pyqasm.loads(program)
90+
module.unroll()
91+
program_str = pyqasm.dumps(module)
92+
result = device.run(program_str, shots=1000)

0 commit comments

Comments
 (0)