CommutativeOptimization
QuICT.qcda.optimization.commutative_optimization.CommutativeOptimization ¶
Bases: object
Optimize the given Circuit/CompositeGate by merging the adjacent gates with the commutative relation between gates in consideration.
During the process, several parameterization and deparameterization process could be included, as listed
'x': Rx <--> X, SX, SX_dagger
'y': Ry <--> Y, SY, SY_dagger
'z': Rz <--> Z, S, T, S_dagger, T_dagger
Whether to parameterize or deparameterize certain kinds of gates could be specified
by listing them in para and depara, where ['all'] stands for ['x', 'y', 'z'] for convenience.
Examples:
>>> from QuICT.qcda.optimization import CommutativeOptimization
>>> CO = CommutativeOptimization()
>>> circ_opt = CO.execute(circ)
Parameters:
-
para(List[str], default:['all']) –parameterize which kinds of gates
-
depara(List[str], default:[]) –deparameterize which kinds of gates
-
keep_phase(bool, default:False) –whether to keep the global phase as a GPhase gate in the output
Source code in QuICT/qcda/optimization/commutative_optimization/commutative_optimization.py
combine
staticmethod
¶
Combine gate_x and gate_y of the same type
Generally, the combination could be divided into four categories:
1. Elimination: the combined gate is ID
2. Addition: the parameters of gates should be added
3. Multiplication: the matrices of gates should be multiplied(i.e. UnitaryGate)
4. Other: some special case(e.g. SS=Z) or not able to be calculated easily(e.g. U3Gate)
In this method we would only deal with the first 3 cases, while the last case is partially
handled by preprocessing the parameterize function.
Parameters:
Returns:
-
BasicGate(BasicGate) –The combined gate
Raises:
-
TypeError–If the input gates are not of the same type or unknown gate type encountered.
-
ValueError–If the input gates are not operating on the same qubits in the same way or could not be combined directly to a gate with the same type.
Source code in QuICT/qcda/optimization/commutative_optimization/commutative_optimization.py
deparameterize
classmethod
¶
Deparameterize the parameterized gates if possible, as an inverse process of
parameterize function.
Be aware that gates like Rz(3*np.pi/4) would be transformed to S.T (which would cause more gates).
Parameters:
-
gate(BasicGate) –Gate to be transformed to its 'deparameterized' version
-
depara(List[str], default:['x', 'y', 'z']) –deparameterize which kinds of gates
Returns:
-
Tuple[CompositeGate, float]–Tuple[CompositeGate, float]: If deparameterization process is possible, the 'deparameterized' version of the gate with the phase angle derived in the process will be returned. Otherwise, the
gateitself with phase angle 0 will be returned.
Source code in QuICT/qcda/optimization/commutative_optimization/commutative_optimization.py
execute ¶
Optimize the given Circuit/CompositeGate by merging the adjacent gates with the commutative relation between gates in consideration.
WARNING: This method is implemented for Circuit/CompositeGate with BasicGates only (say, ComplexGates are not supported), other gates in the Circuit/ CompositeGate may result in an exception or unexpected output.
FIXME: Merging gates may cause the modification of commutative relation. In this version only the simplest (also the most common) case, i.e. the merged gate is identity, is handled. More specified analysis of the DAG is needed to deal with other cases, which is postponed until the graph structure is completed.
Parameters:
-
gates(Union[Circuit, CompositeGate]) –Circuit/CompositeGate to be optimized
Returns:
-
CompositeGate(CompositeGate) –The CompositeGate after optimization
Source code in QuICT/qcda/optimization/commutative_optimization/commutative_optimization.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
parameterize
classmethod
¶
In BasicGates, (X, SX, SX_dagger), (Y, SY, SY_dagger), (Z, S, Sdagger, T, Tdagger) could be
'parameterized' to Rx, Ry, Rz respectively, which is helpful in the
combine function.
Parameters:
-
gate(BasicGate) –Gate to be transformed to its 'parameterized' version
-
para(List[str], default:['x', 'y', 'z']) –parameterize which kinds of gates
Returns:
-
Tuple[BasicGate, float]–Tuple[BasicGate, float]: If the
gateis listed above, its 'parameterized' version with the phase angle derived in the process will be returned. Otherwise, thegateitself with phase angle 0 will be returned.
Source code in QuICT/qcda/optimization/commutative_optimization/commutative_optimization.py
QuICT.qcda.optimization.commutative_optimization.commutative_optimization.Node ¶
Bases: object
(Temporary) implementation of Directed Acyclic Graph (DAG) used in this code
Attributes:
-
gate(BasicGate) –Gate represented by the node
-
identity(bool) –Whether the gate is identity (upon a global phase)
-
predecessor(list[int]) –Predecessors of the node
-
reachable(bool) –Whether this node needs to be compared with the new node
TODO: Replace this part with a graph structure
Parameters:
-
gate(BasicGate) –Gate represented by the node