Skip to content

Commit 0cb34e5

Browse files
committed
pass tests
1 parent 734724b commit 0cb34e5

File tree

3 files changed

+179
-9
lines changed

3 files changed

+179
-9
lines changed

demo.ipynb

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"vscode": {
8+
"languageId": "shellscript"
9+
}
10+
},
11+
"outputs": [
12+
{
13+
"name": "stdout",
14+
"output_type": "stream",
15+
"text": [
16+
"Processing /Users/churchill/GitHub/qutip-qip\n",
17+
" Installing build dependencies ... \u001b[?25ldone\n",
18+
"\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n",
19+
"\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
20+
"\u001b[?25hCollecting numpy>=1.16.6 (from qutip-qip==0.4.0.dev0+734724b)\n",
21+
" Using cached numpy-1.26.4-cp311-cp311-macosx_11_0_arm64.whl.metadata (114 kB)\n",
22+
"Collecting scipy>=1.0 (from qutip-qip==0.4.0.dev0+734724b)\n",
23+
" Using cached scipy-1.12.0-cp311-cp311-macosx_12_0_arm64.whl.metadata (165 kB)\n",
24+
"Collecting qutip>=4.6 (from qutip-qip==0.4.0.dev0+734724b)\n",
25+
" Using cached qutip-4.7.5-cp311-cp311-macosx_14_0_arm64.whl\n",
26+
"Collecting packaging (from qutip-qip==0.4.0.dev0+734724b)\n",
27+
" Using cached packaging-24.0-py3-none-any.whl.metadata (3.2 kB)\n",
28+
"Using cached numpy-1.26.4-cp311-cp311-macosx_11_0_arm64.whl (14.0 MB)\n",
29+
"Using cached scipy-1.12.0-cp311-cp311-macosx_12_0_arm64.whl (31.4 MB)\n",
30+
"Using cached packaging-24.0-py3-none-any.whl (53 kB)\n",
31+
"Building wheels for collected packages: qutip-qip\n",
32+
" Building wheel for qutip-qip (pyproject.toml) ... \u001b[?25ldone\n",
33+
"\u001b[?25h Created wheel for qutip-qip: filename=qutip_qip-0.4.0.dev0+734724b-py3-none-any.whl size=131781 sha256=8b0fbe91254a862f234a95a2e3ca7adcd6ba613922075128ac5c173f8a15e0af\n",
34+
" Stored in directory: /Users/churchill/Library/Caches/pip/wheels/9a/16/a2/f9c88fc86676e21805aa95320fa839b4e72041d5d251271ac3\n",
35+
"Successfully built qutip-qip\n",
36+
"Installing collected packages: packaging, numpy, scipy, qutip, qutip-qip\n",
37+
" Attempting uninstall: packaging\n",
38+
" Found existing installation: packaging 24.0\n",
39+
" Uninstalling packaging-24.0:\n",
40+
" Successfully uninstalled packaging-24.0\n",
41+
" Attempting uninstall: numpy\n",
42+
" Found existing installation: numpy 1.26.4\n",
43+
" Uninstalling numpy-1.26.4:\n",
44+
" Successfully uninstalled numpy-1.26.4\n",
45+
" Attempting uninstall: scipy\n",
46+
" Found existing installation: scipy 1.12.0\n",
47+
" Uninstalling scipy-1.12.0:\n",
48+
" Successfully uninstalled scipy-1.12.0\n",
49+
" Attempting uninstall: qutip\n",
50+
" Found existing installation: qutip 4.7.5\n",
51+
" Uninstalling qutip-4.7.5:\n",
52+
" Successfully uninstalled qutip-4.7.5\n",
53+
" Attempting uninstall: qutip-qip\n",
54+
" Found existing installation: qutip-qip 0.4.0.dev0+734724b\n",
55+
" Uninstalling qutip-qip-0.4.0.dev0+734724b:\n",
56+
" Successfully uninstalled qutip-qip-0.4.0.dev0+734724b\n",
57+
"Successfully installed numpy-1.26.4 packaging-24.0 qutip-4.7.5 qutip-qip-0.4.0.dev0+734724b scipy-1.12.0\n",
58+
"Note: you may need to restart the kernel to use updated packages.\n"
59+
]
60+
}
61+
],
62+
"source": [
63+
"# pip install the current directory, overwriting any existing installation\n",
64+
"%pip install --upgrade --force-reinstall ."
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"## IonQ Integration using Qiskit"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 2,
77+
"metadata": {},
78+
"outputs": [
79+
{
80+
"name": "stderr",
81+
"output_type": "stream",
82+
"text": [
83+
"/Users/churchill/GitHub/qutip-qip/venv/lib/python3.11/site-packages/qutip/qip/circuit.py:242: UserWarning: Unknown gate H\n",
84+
" warnings.warn(\"Unknown gate %s\" % name)\n"
85+
]
86+
},
87+
{
88+
"data": {
89+
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAJcAAABkCAQAAAAhkGpFAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAACYktHRAD/h4/MvwAAAAlwSFlzAAAAZAAAAGQAD5bF3QAAAAd0SU1FB+gDFA8mLFRCIa0AAAL7SURBVHja7dw/a1NRHMbx708qLhWNFlyESMDZP3F3uZuLDnF3SV9C8hLSNyA0o5vNoC8gL8DBDs7SQMHBqVexCE7HwST2j0Keck5LL8+nQ5NwOSf3m5tz25LbYECL0h7wqfgcf7X5xvcyQ0c6x/24/K5c9BO4XJxL4lwS55I4l8S5JM4lcS6Jc0mcS+JcEueSOJfEuSTOJXEuiXNJnEviXBLnkjiXxLkkziVxLolzSZxL4lwS55I4l8S5JM4lcS6Jc0mcS+JcEueSOJekYbmiFweRYi+6hcanew6fq7/P5+JzAKzzhusAfOFViQnWqBp0GUJ7Hgvu8oxf+Sdo1GUI0eJgfrNOt0rM0Ki1K9VsAvBj/j27Rh1dANHiNW/T+zKjN+roAkg1+xyWGr1xucpyLolzSZxL4lwS55I4l8S5JM4lcS6Jc0mcS+JcEueSOJfEuSTOJXEuiXNJnEviXBLnkjiXxLkkziVxLolzSZxL4lwS55I4l8S5JM4lcS6Jc0nW8g4XD9lYcdP1Uh+3LSlzLgbA/kpbPg0uX7DcufaZpukqG8ao3OeTy/HaJcl9dF2w6NOlx4t4xyTt5h+/aK7o0AdglsbRpwPAVqqLzbZNzZiaD1xlJyZpmHuOom/GNEtDBkzTGNKYFqRhqVjADpP0Mk2BwzThCa0YXapcEBX1cumvKPD2WM40+vOyLKSaIb3o5J2l9FJfMY8VHTqsdM48ox5bxx9INVvzxSCbtch7tWyb9WP3u8yiAqBi99gb8QaPIueeJLqxeA6LkX/yPLK+RLmvlr13Ile1vLKwe+LYusljbmeceYNq+RyYj3yNO8tH80hZvxhRHblXcbC8vUfv/1tmmHnv9Mj0GOXdv7Jr1/mtXDCNwanH+rnnLJuruzwXnly58hvSP/7vEGJw5KycSbFcUcWIik5UECP6tP7x6meUajbZWcwRndg5sm5mnKbg2pVnS2H2Ftvs8ZUZH+nnHj2RmvU74/JK/2L8FwlJ7qNr9R8+2xe962fxG0g2oKzcZUZvAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDI0LTAzLTIwVDE1OjM4OjQ0KzAwOjAwXUe95QAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyNC0wMy0yMFQxNTozODo0NCswMDowMCwaBVkAAAAodEVYdGRhdGU6dGltZXN0YW1wADIwMjQtMDMtMjBUMTU6Mzg6NDQrMDA6MDB7DySGAAAAE3RFWHRwZGY6VmVyc2lvbgBQREYtMS41UzZawQAAAABJRU5ErkJggg==",
90+
"text/plain": [
91+
"<IPython.core.display.Image object>"
92+
]
93+
},
94+
"execution_count": 2,
95+
"metadata": {},
96+
"output_type": "execute_result"
97+
}
98+
],
99+
"source": [
100+
"from qutip.qip.circuit import QubitCircuit\n",
101+
"\n",
102+
"qc = QubitCircuit(N=2, num_cbits=1)\n",
103+
"qc.add_gate(\"H\", targets=0)\n",
104+
"qc.add_gate(\"CNOT\", controls=1, targets=0)\n",
105+
"\n",
106+
"qc.png"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 3,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"name": "stdout",
116+
"output_type": "stream",
117+
"text": [
118+
"{'0': 512, '3': 512}\n"
119+
]
120+
}
121+
],
122+
"source": [
123+
"from qutip_qip.ionq import Provider, IonQSimulator\n",
124+
"\n",
125+
"provider = Provider()\n",
126+
"backend = IonQSimulator(provider=provider)\n",
127+
"\n",
128+
"job = backend.run(qc)\n",
129+
"results = job.get_results()\n",
130+
"print(results)"
131+
]
132+
}
133+
],
134+
"metadata": {
135+
"kernelspec": {
136+
"display_name": "venv",
137+
"language": "python",
138+
"name": "python3"
139+
},
140+
"language_info": {
141+
"codemirror_mode": {
142+
"name": "ipython",
143+
"version": 3
144+
},
145+
"file_extension": ".py",
146+
"mimetype": "text/x-python",
147+
"name": "python",
148+
"nbconvert_exporter": "python",
149+
"pygments_lexer": "ipython3",
150+
"version": "3.11.8"
151+
}
152+
},
153+
"nbformat": 4,
154+
"nbformat_minor": 2
155+
}

src/qutip_qip/ionq/converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ def convert_qutip_circuit(qc: QubitCircuit) -> dict:
1717
"""
1818
ionq_circuit = []
1919
for gate in qc.gates:
20-
if isinstance(gate, Gate):
20+
if (
21+
hasattr(gate, "name")
22+
and hasattr(gate, "targets")
23+
and hasattr(gate, "controls")
24+
):
2125
ionq_circuit.append(
2226
{
2327
"gate": gate.name,

tests/test_ionq.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from unittest.mock import patch, MagicMock
3-
from qutip.qip.circuit import QubitCircuit, Gate
4-
from qutip.ionq import (
3+
from qutip_qip.circuit import QubitCircuit, Gate
4+
from qutip_qip.ionq import (
55
Provider,
66
IonQSimulator,
77
IonQQPU,
@@ -14,24 +14,28 @@ class TestConverter(unittest.TestCase):
1414
def test_convert_qutip_circuit(self):
1515
# Create a simple QubitCircuit with one gate for testing
1616
qc = QubitCircuit(N=1)
17-
qc.add_gate("X", targets=[0])
17+
qc.add_gate("H", targets=[0])
18+
qc.add_gate("CNOT", targets=[0], controls=[1])
1819
# Convert the qutip_qip circuit to IonQ format
1920
ionq_circuit = convert_qutip_circuit(qc)
20-
expected_output = [{"gate": "X", "targets": [0], "controls": []}]
21+
expected_output = [
22+
{"gate": "H", "targets": [0], "controls": []},
23+
{"gate": "CNOT", "targets": [0], "controls": [1]},
24+
]
2125
self.assertEqual(ionq_circuit, expected_output)
2226

2327

2428
class TestIonQBackend(unittest.TestCase):
2529
def setUp(self):
2630
self.provider = Provider(token="dummy_token")
2731

28-
@patch("qutip.ionq.Provider")
32+
@patch("qutip_qip.ionq.Provider")
2933
def test_simulator_initialization(self, mock_provider):
3034
simulator = IonQSimulator(provider=mock_provider)
3135
self.assertEqual(simulator.provider, mock_provider)
3236
self.assertEqual(simulator.gateset, "qis")
3337

34-
@patch("qutip.ionq.Provider")
38+
@patch("qutip_qip.ionq.Provider")
3539
def test_qpu_initialization(self, mock_provider):
3640
qpu = IonQQPU(provider=mock_provider, qpu="harmony")
3741
self.assertEqual(qpu.provider, mock_provider)
@@ -55,8 +59,15 @@ def test_submit(self, mock_post):
5559

5660
@patch("requests.get")
5761
def test_get_results(self, mock_get):
58-
mock_get.return_value.json.return_value = {"0": 0.5, "1": 0.5}
59-
mock_get.return_value.status_code = 200
62+
# Simulate the status check response and the final result response
63+
mock_get.side_effect = [
64+
MagicMock(
65+
json=lambda: {"status": "completed"}
66+
), # Simulated status check response
67+
MagicMock(
68+
json=lambda: {"0": 0.5, "1": 0.5}
69+
), # Simulated final results response
70+
]
6071
job = Job(
6172
circuit={},
6273
shots=1024,

0 commit comments

Comments
 (0)