Commit ac53148
authored
Merge pull request #328 from Refactor Gate class (2)
The original PR #325 was split into 2, for the previous set of changes look at that PR description.
- Defined `__slots__` for all the Abstract Gate classes and standard gate classes for better memory efficiency and faster lookup time.
- Added boolean `is_controlled`, `is_parametric` methods to the `Gate` class.
- Added the concept and implementation of namespaces for operations. This is done because at several places earlier gate.name was being used for comparison. Each operation (gate for now) must now be defined in a namespace or if it is a temporary gate, namespace can be set to None. This allows the user to define a faulty version of standard gates or follow their own convention for certain Parametric gates. The concept of namespace is much broader. Currently the parent namespace is "std", under it only "gates" is set as of now. In future we can make other namespaces under "std" like for Arithmetic Ops, Ansatz Ops etc. For the idea of Ops look at #263 (comment) Though it hasn't been implemented yet. In a way std namespace will work like a standard library for different operations and the user can also define their own implementation for operations in a different namespace.
- Added caching in `get_qobj` method for standard gates. This is done for two reasons.
```
@staticmethod
def get_qobj() -> Qobj:
return Qobj([[1, 0], [0, -1]])
```
If you run it multiple times and check `id()` for each obj, they will be different. This leads to higher memory consumption for large circuit simulations, even though all of them represent the Z gate's qobj in this case. Additonally different qobjs will each need to be garbage collected. Memory and lookup are an even larger bottleneck in VQA circuits where several times many gates share the same parameter.
- Added `dtype` to `get_qobj(dtype="dense")` method, . This might be useful if the circuit simulation is run on a different backend like JAX, CUDA.
- Made `Gate` non initialised by default i.e. `__init__` raise will raise an Error. Obviously`ParametricGate` and its controlled versions are initialisable. This makes sense because target, controls have been removed from gates and `x1=X()`, `x2=X()` now represent the same thing. Also good from a memory point of view.
- Added `Sdag`, `Tdag`, `SQRTXdag`, `ISWAPdag`, `SQRTSWAPdag`, `BERKELEYdag` to the standard gates. They have been added to the documentation table and in the circuit draw (colour config for them).
- Added `inverse` method for `Gate` class. Also implemented inverse for all standard gates and tests for the same. For controlled gates, the inverse is auto calculated based on the target gate. Closes #301
- Renamed `unitary_gate` to `get_unitary_gate` and added checks for all the input arguments.
- The control value for a gate is now baked into the control gate itself. The user can always generate his own version of controlled gate e.g. `controlled(gates.X, num_ctrl_qubits=2, control_value=0b10)`. For more detail check out #308 (comment)
- For all Controlled gates, the (target_gate, num_ctrl_qubit, ctrl_value) is used as another key in the namespace to reference that controlled gate. This is done in order to prevent regeneration of the controlled gate class for a given target gate with known (num_ctrl_qubit, ctrl_value). Since the namespace is used by default, there is no overhead.
- Renamed `controlled_gate` to `controlled_gate_unitary` and resolved the bug which didn't allow for generating the correct qobj if number of controls > 1.
- Added gate equivalence checking for `ParametricGate`, `ControlledGate`. And added `__eq__`, `__hash__` in Gate metaclass as well. Replaced gate.name = "X" search with gate == X now.
- Corrected inconsistency between Gate and Type[Gate] from previous PRs. Gate refers to an instantiated object while type[Gate] refers to a Gate class or subclass in accordance with Python's typehinting.
- Added several more checks in `__init_subclass__`, `add_gate` method.
- Added tests in `test_gates.py` and `test_circuit.py` for mostly incorrect cases like num_qubits being negative etc., which should raise an Error. Since Gates are used throughout the codebase, their normal usage is already mostly covered.
**Maintainence and Bug Fixes:**
- `gate_product` had a clear bug that `tensor_list` was not defined but was being used, fixed that and also moved the entire `gate_product` logic to operations, which was split in circuit-simulator utils, operations but was always being imported from operations as per `__init__` even in `mat_mul_simulator.py`.
- Removed deprecated arguments and functions that were deprecated 3 years back. This was checked in the git commit history.
- Remove mutable default values from function arguments across the codebase, this is a common Python bug. Something like targets = [], can often lead to an error. For details check out this blog https://medium.com/@matbrizolla/beware-of-the-hidden-trap-understanding-mutable-default-arguments-in-python-d10e8b0a7b72
- Corrected several `isInstance` checks in the codebase. `Iterable` and `Sequence` were being used interchangeably at several places especially in qasm. This led to several unexpected behaviours and errors during gate refactoring.
- Moved pytest.ini to the root, this is what pytest themselves recommend https://docs.pytest.org/en/stable/explanation/goodpractices.html#choosing-a-test-layout-import-rules . The reason was that placing pytest.ini in `tests/` led to certain sys path issues, for reference check https://docs.pytest.org/en/stable/explanation/pythonpath.html. This caused an error when standard gates were designated to a namespace and pytest was trying to redefine them and reassign them to the same namespace.
*Edit: Some more changes*
- Separated IDLE (meant for Pulse level simulation) and Identity gate (standard Identity matrix).
- In Pulse compiler instead of `gate.name`, `gates` are themselves being used as keys.
- Removed expand argument from `inverse()` in Parametric Gates.
- Renamed `controlled` to `get_controlled_gate`.File tree
81 files changed
+5498
-3500
lines changed- doc
- pulse-paper
- source
- apidoc
- src/qutip_qip
- algorithms
- circuit
- draw
- simulator
- compiler
- decompose
- device
- noise
- operations
- gates
- std
- pulse
- qiskit
- utils
- transpiler
- tests
- decomposition_functions
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
81 files changed
+5498
-3500
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | 142 | | |
147 | 143 | | |
148 | 144 | | |
| 145 | + | |
149 | 146 | | |
| 147 | + | |
150 | 148 | | |
| 149 | + | |
151 | 150 | | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
158 | 159 | | |
159 | 160 | | |
| 161 | + | |
160 | 162 | | |
| 163 | + | |
161 | 164 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
166 | 171 | | |
167 | 172 | | |
| 173 | + | |
168 | 174 | | |
| 175 | + | |
169 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
170 | 181 | | |
| 182 | + | |
| 183 | + | |
171 | 184 | | |
172 | | - | |
173 | | - | |
| 185 | + | |
| 186 | + | |
174 | 187 | | |
175 | 188 | | |
176 | 189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
| 10 | + | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
16 | | - | |
17 | | - | |
18 | 15 | | |
19 | 16 | | |
20 | 17 | | |
21 | | - | |
22 | | - | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
| 7 | + | |
9 | 8 | | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
69 | | - | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| |||
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
116 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
117 | 121 | | |
118 | 122 | | |
119 | 123 | | |
| |||
136 | 140 | | |
137 | 141 | | |
138 | 142 | | |
139 | | - | |
140 | | - | |
| 143 | + | |
| 144 | + | |
141 | 145 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
64 | | - | |
65 | | - | |
66 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
67 | 68 | | |
68 | | - | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | 10 | | |
39 | 11 | | |
40 | 12 | | |
| |||
0 commit comments