1
1
import os
2
2
3
- from numpy .testing import (assert_ , assert_allclose ,
4
- assert_equal )
3
+ from numpy .testing import assert_ , assert_allclose , assert_equal
5
4
import numpy as np
6
5
import pytest
7
6
7
+ pytest .importorskip ("qutip_qtrl" )
8
8
from qutip_qip .device import OptPulseProcessor , SpinChainModel
9
9
from qutip_qip .circuit import QubitCircuit
10
10
from qutip_qip .qubits import qubit_states
11
11
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
+ )
14
21
from qutip_qip .operations import (
15
- cnot , hadamard_transform , expand_operator , gate_sequence_product )
22
+ cnot ,
23
+ )
16
24
import qutip
17
25
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" ):
19
28
from qutip import Options as SolverOptions
20
29
else :
21
30
from qutip import SolverOptions
@@ -38,7 +47,8 @@ def test_simple_hadamard(self):
38
47
test = OptPulseProcessor (N , drift = H_d )
39
48
test .add_control (H_c , targets = 0 )
40
49
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
+ )
42
52
43
53
# test run_state
44
54
rho0 = qubit_states (1 , [0 ])
@@ -51,36 +61,35 @@ def test_multi_qubits(self):
51
61
Test for multi-qubits system.
52
62
"""
53
63
N = 3
54
- H_d = tensor ([sigmaz ()]* 3 )
64
+ H_d = tensor ([sigmaz ()] * 3 )
55
65
H_c = []
56
66
57
67
# test empty ctrls
58
68
num_tslots = 30
59
69
evo_time = 10
60
70
test = OptPulseProcessor (N )
61
71
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 )
64
73
# test periodically adding ctrls
65
74
sx = sigmax ()
66
75
iden = identity (2 )
67
- assert ( len (test .get_control_labels ()) == 3 )
76
+ assert len (test .get_control_labels ()) == 3
68
77
test .add_control (sigmax (), cyclic_permutation = True )
69
78
test .add_control (sigmay (), cyclic_permutation = True )
70
79
71
80
# test pulse genration for cnot gate, with kwargs
72
81
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
+ )
75
85
rho0 = qubit_states (3 , [1 , 1 , 1 ])
76
86
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 )
80
89
81
90
def test_multi_gates (self ):
82
91
N = 2
83
- H_d = tensor ([sigmaz ()]* 2 )
92
+ H_d = tensor ([sigmaz ()] * 2 )
84
93
H_c = []
85
94
86
95
test = OptPulseProcessor (N )
@@ -90,22 +99,23 @@ def test_multi_gates(self):
90
99
test .add_control (tensor ([sigmay (), sigmay ()]))
91
100
92
101
# 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
+ }
96
107
qc = QubitCircuit (N )
97
108
qc .add_gate ("SNOT" , 0 )
98
109
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 )
102
112
103
113
rho0 = rand_ket (4 ) # use random generated ket state
104
114
rho0 .dims = [[2 , 2 ], [1 , 1 ]]
105
115
U = qc .compute_unitary ()
106
116
rho1 = U * rho0
107
117
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 )
109
119
110
120
def test_with_model (self ):
111
121
model = SpinChainModel (3 , setup = "linear" )
@@ -117,7 +127,7 @@ def test_with_model(self):
117
127
qc , merge_gates = True , num_tslots = 10 , evo_time = 2.0
118
128
)
119
129
120
- if parse_version (qutip .__version__ ) < parse_version (' 5.dev' ):
130
+ if parse_version (qutip .__version__ ) < parse_version (" 5.dev" ):
121
131
init_state = qutip .rand_ket (8 , dims = [[2 , 2 , 2 ], [1 , 1 , 1 ]])
122
132
else :
123
133
init_state = qutip .rand_ket ([2 , 2 , 2 ])
0 commit comments