Skip to content

Commit e393b43

Browse files
authored
Merge pull request #242 from BoxiLi/fix_qtrl_error
Use qutip_qtrl master branch and make tests optional
2 parents 87a07ec + 77fe60d commit e393b43

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

.github/workflows/test.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ jobs:
6161
if: ${{ startsWith( matrix.qutip-version, '@') }}
6262
run: |
6363
python -m pip install 'git+https://github.com/qutip/qutip.git${{ matrix.qutip-version }}'
64+
65+
- name: Install qutip-qtrl from GitHub
66+
if: ${{ matrix.qutip-version == '@master' }}
67+
run: |
68+
python -m pip install 'git+https://github.com/qutip/qutip-qtrl.git${{ matrix.qutip-version }}'
6469
6570
- name: Install Qiskit from PyPI
6671
if: ${{ matrix.qiskit-version != '' }}
@@ -69,7 +74,8 @@ jobs:
6974
- name: Install qutip-qip
7075
# Installing in-place so that coveralls can locate the source code.
7176
run: |
72-
pip install -e .[full]
77+
pip install matplotlib pytest
78+
pip install -e .
7379
- name: Test with pytest and generate coverage report
7480
run: |
7581
pip install pytest-cov coveralls

src/qutip_qip/device/optpulseprocessor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import numpy as np
66

77
from qutip import Qobj, identity, tensor, mesolve
8-
import qutip.control.pulseoptim as cpo
98
from ..circuit import QubitCircuit
109
from .processor import Processor
1110
from ..operations import gate_sequence_product, expand_operator
@@ -195,6 +194,8 @@ def load_circuit(
195194
),
196195
)
197196

197+
import qutip.control.pulseoptim as cpo
198+
198199
result = cpo.optimize_pulse_unitary(
199200
full_drift_ham, full_ctrls_hams, U_0, U_targ, **kwargs
200201
)

tests/test_optpulseprocessor.py

+35-25
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import os
22

3-
from numpy.testing import (assert_, assert_allclose,
4-
assert_equal)
3+
from numpy.testing import assert_, assert_allclose, assert_equal
54
import numpy as np
65
import pytest
76

7+
pytest.importorskip("qutip_qtrl")
88
from qutip_qip.device import OptPulseProcessor, SpinChainModel
99
from qutip_qip.circuit import QubitCircuit
1010
from qutip_qip.qubits import qubit_states
1111
import qutip
12-
from qutip import (fidelity, Qobj, tensor, rand_ket, basis, sigmaz,
13-
sigmax, sigmay, identity, destroy)
12+
from qutip import (
13+
fidelity,
14+
tensor,
15+
rand_ket,
16+
sigmaz,
17+
sigmax,
18+
sigmay,
19+
identity,
20+
)
1421
from qutip_qip.operations import (
15-
cnot, hadamard_transform, expand_operator, gate_sequence_product)
22+
cnot,
23+
)
1624
import qutip
1725
from packaging.version import parse as parse_version
18-
if parse_version(qutip.__version__) < parse_version('5.dev'):
26+
27+
if parse_version(qutip.__version__) < parse_version("5.dev"):
1928
from qutip import Options as SolverOptions
2029
else:
2130
from qutip import SolverOptions
@@ -38,7 +47,8 @@ def test_simple_hadamard(self):
3847
test = OptPulseProcessor(N, drift=H_d)
3948
test.add_control(H_c, targets=0)
4049
tlist, coeffs = test.load_circuit(
41-
qc, num_tslots=num_tslots, evo_time=evo_time, verbose=True)
50+
qc, num_tslots=num_tslots, evo_time=evo_time, verbose=True
51+
)
4252

4353
# test run_state
4454
rho0 = qubit_states(1, [0])
@@ -51,36 +61,35 @@ def test_multi_qubits(self):
5161
Test for multi-qubits system.
5262
"""
5363
N = 3
54-
H_d = tensor([sigmaz()]*3)
64+
H_d = tensor([sigmaz()] * 3)
5565
H_c = []
5666

5767
# test empty ctrls
5868
num_tslots = 30
5969
evo_time = 10
6070
test = OptPulseProcessor(N)
6171
test.add_drift(H_d, [0, 1, 2])
62-
test.add_control(tensor([sigmax(), sigmax()]),
63-
cyclic_permutation=True)
72+
test.add_control(tensor([sigmax(), sigmax()]), cyclic_permutation=True)
6473
# test periodically adding ctrls
6574
sx = sigmax()
6675
iden = identity(2)
67-
assert(len(test.get_control_labels()) == 3)
76+
assert len(test.get_control_labels()) == 3
6877
test.add_control(sigmax(), cyclic_permutation=True)
6978
test.add_control(sigmay(), cyclic_permutation=True)
7079

7180
# test pulse genration for cnot gate, with kwargs
7281
qc = [tensor([identity(2), cnot()])]
73-
test.load_circuit(qc, num_tslots=num_tslots,
74-
evo_time=evo_time, min_fid_err=1.0e-6)
82+
test.load_circuit(
83+
qc, num_tslots=num_tslots, evo_time=evo_time, min_fid_err=1.0e-6
84+
)
7585
rho0 = qubit_states(3, [1, 1, 1])
7686
rho1 = qubit_states(3, [1, 1, 0])
77-
result = test.run_state(
78-
rho0, options=SolverOptions(store_states=True))
79-
assert_(fidelity(result.states[-1], rho1) > 1-1.0e-6)
87+
result = test.run_state(rho0, options=SolverOptions(store_states=True))
88+
assert_(fidelity(result.states[-1], rho1) > 1 - 1.0e-6)
8089

8190
def test_multi_gates(self):
8291
N = 2
83-
H_d = tensor([sigmaz()]*2)
92+
H_d = tensor([sigmaz()] * 2)
8493
H_c = []
8594

8695
test = OptPulseProcessor(N)
@@ -90,22 +99,23 @@ def test_multi_gates(self):
9099
test.add_control(tensor([sigmay(), sigmay()]))
91100

92101
# qubits circuit with 3 gates
93-
setting_args = {"SNOT": {"num_tslots": 10, "evo_time": 1},
94-
"SWAP": {"num_tslots": 30, "evo_time": 3},
95-
"CNOT": {"num_tslots": 30, "evo_time": 3}}
102+
setting_args = {
103+
"SNOT": {"num_tslots": 10, "evo_time": 1},
104+
"SWAP": {"num_tslots": 30, "evo_time": 3},
105+
"CNOT": {"num_tslots": 30, "evo_time": 3},
106+
}
96107
qc = QubitCircuit(N)
97108
qc.add_gate("SNOT", 0)
98109
qc.add_gate("SWAP", targets=[0, 1])
99-
qc.add_gate('CNOT', controls=1, targets=[0])
100-
test.load_circuit(qc, setting_args=setting_args,
101-
merge_gates=False)
110+
qc.add_gate("CNOT", controls=1, targets=[0])
111+
test.load_circuit(qc, setting_args=setting_args, merge_gates=False)
102112

103113
rho0 = rand_ket(4) # use random generated ket state
104114
rho0.dims = [[2, 2], [1, 1]]
105115
U = qc.compute_unitary()
106116
rho1 = U * rho0
107117
result = test.run_state(rho0)
108-
assert_(fidelity(result.states[-1], rho1) > 1-1.0e-6)
118+
assert_(fidelity(result.states[-1], rho1) > 1 - 1.0e-6)
109119

110120
def test_with_model(self):
111121
model = SpinChainModel(3, setup="linear")
@@ -117,7 +127,7 @@ def test_with_model(self):
117127
qc, merge_gates=True, num_tslots=10, evo_time=2.0
118128
)
119129

120-
if parse_version(qutip.__version__) < parse_version('5.dev'):
130+
if parse_version(qutip.__version__) < parse_version("5.dev"):
121131
init_state = qutip.rand_ket(8, dims=[[2, 2, 2], [1, 1, 1]])
122132
else:
123133
init_state = qutip.rand_ket([2, 2, 2])

0 commit comments

Comments
 (0)