|
25 | 25 | from pennylane.estimator import CompressedResourceOp, ResourceOperator, Resources |
26 | 26 | from pennylane.estimator.resource_operator import ( |
27 | 27 | GateCount, |
28 | | - ResourcesUndefinedError, |
29 | 28 | _dequeue, |
30 | 29 | _make_hashable, |
31 | 30 | resource_rep, |
@@ -304,6 +303,67 @@ class CNOT(DummyOp): |
304 | 303 | num_wires = 2 |
305 | 304 |
|
306 | 305 |
|
| 306 | +class DummyOp_decomps(ResourceOperator): |
| 307 | + """Class for testing the resource_decomp methods""" |
| 308 | + |
| 309 | + resource_keys = {"max_register_size"} |
| 310 | + |
| 311 | + def __init__(self, max_register_size, wires=None): |
| 312 | + self.max_register_size = max_register_size |
| 313 | + self.num_wires = 2 * max_register_size |
| 314 | + super().__init__(wires=wires) |
| 315 | + |
| 316 | + @property |
| 317 | + def resource_params(self): |
| 318 | + r"""Returns a dictionary containing the minimal information needed to compute the resources.""" |
| 319 | + return {"max_register_size": self.max_register_size} |
| 320 | + |
| 321 | + @classmethod |
| 322 | + def resource_rep(cls, max_register_size): |
| 323 | + r"""Returns a compressed representation containing only the parameters of |
| 324 | + the Operator that are needed to compute the resources. |
| 325 | + """ |
| 326 | + num_wires = 2 * max_register_size |
| 327 | + return CompressedResourceOp(cls, num_wires, {"max_register_size": max_register_size}) |
| 328 | + |
| 329 | + @classmethod |
| 330 | + def resource_decomp(cls, max_register_size): |
| 331 | + r"""Returns a dictionary representing the resources of the operator. The |
| 332 | + keys are the operators and the associated values are the counts. |
| 333 | + """ |
| 334 | + rx = resource_rep(RX, {"x": 1}) |
| 335 | + return [GateCount(rx, max_register_size)] |
| 336 | + |
| 337 | + @classmethod |
| 338 | + def controlled_resource_decomp(cls, num_ctrl_wires, num_zero_ctrl, target_resource_params): |
| 339 | + r"""Returns a list representing the resources of the operator. Each object in the list represents a gate and the |
| 340 | + number of times it occurs in the circuit. |
| 341 | + """ |
| 342 | + max_register_size = target_resource_params["max_register_size"] |
| 343 | + cnot = resource_rep(CNOT, {"x": None}) |
| 344 | + rx = resource_rep(RX, {"x": 1}) |
| 345 | + return [GateCount(cnot, max_register_size), GateCount(rx, max_register_size)] |
| 346 | + |
| 347 | + @classmethod |
| 348 | + def adjoint_resource_decomp(cls, target_resource_params): |
| 349 | + r"""Returns a list representing the resources of the operator. Each object in the list represents a gate and the |
| 350 | + number of times it occurs in the circuit. |
| 351 | + """ |
| 352 | + max_register_size = target_resource_params["max_register_size"] |
| 353 | + rx = resource_rep(RX, {"x": 1}) |
| 354 | + h = resource_rep(Hadamard, {"x": None}) |
| 355 | + return [GateCount(rx, max_register_size), GateCount(h, 2 * max_register_size)] |
| 356 | + |
| 357 | + @classmethod |
| 358 | + def pow_resource_decomp(cls, pow_z, target_resource_params): |
| 359 | + r"""Returns a list representing the resources of the operator. Each object in the list represents a gate and the |
| 360 | + number of times it occurs in the circuit. |
| 361 | + """ |
| 362 | + max_register_size = target_resource_params["max_register_size"] |
| 363 | + rx = resource_rep(RX, {"x": 1}) |
| 364 | + return [GateCount(rx, max_register_size * pow_z)] |
| 365 | + |
| 366 | + |
307 | 367 | class TestResourceOperator: |
308 | 368 | """Tests for the ResourceOperator class""" |
309 | 369 |
|
@@ -485,22 +545,30 @@ def test_matmul_error(self): |
485 | 545 | def test_default_resource_keys(self): |
486 | 546 | """Test that default resource keys returns the correct result.""" |
487 | 547 | op1 = X |
488 | | - assert op1.resource_keys == set() |
| 548 | + assert op1.resource_keys == set() # pylint: disable=comparison-with-callable |
489 | 549 |
|
490 | 550 | def test_adjoint_resource_decomp(self): |
491 | 551 | """Test that default adjoint operator returns the correct error.""" |
492 | | - with pytest.raises(ResourcesUndefinedError): |
493 | | - X.adjoint_resource_decomp() |
| 552 | + dummy_params = {"max_register_size": 10} |
| 553 | + assert DummyOp_decomps.adjoint_resource_decomp(target_resource_params=dummy_params) == [ |
| 554 | + GateCount(DummyCmprsRep("RX", 1), 10), |
| 555 | + GateCount(DummyCmprsRep("Hadamard", None), 20), |
| 556 | + ] |
494 | 557 |
|
495 | 558 | def test_controlled_resource_decomp(self): |
496 | 559 | """Test that default controlled operator returns the correct error.""" |
497 | | - with pytest.raises(ResourcesUndefinedError): |
498 | | - X.controlled_resource_decomp(ctrl_num_ctrl_wires=2, ctrl_num_ctrl_values=0) |
| 560 | + dummy_params = {"max_register_size": 10} |
| 561 | + assert DummyOp_decomps.controlled_resource_decomp( |
| 562 | + num_ctrl_wires=2, num_zero_ctrl=0, target_resource_params=dummy_params |
| 563 | + ) == [GateCount(DummyCmprsRep("CNOT", None), 10), GateCount(DummyCmprsRep("RX", 1), 10)] |
499 | 564 |
|
500 | 565 | def test_pow_resource_decomp(self): |
501 | 566 | """Test that default power operator returns the correct error.""" |
502 | | - with pytest.raises(ResourcesUndefinedError): |
503 | | - X.pow_resource_decomp(2) |
| 567 | + |
| 568 | + dummy_params = {"max_register_size": 10} |
| 569 | + assert DummyOp_decomps.pow_resource_decomp( |
| 570 | + pow_z=2, target_resource_params=dummy_params |
| 571 | + ) == [GateCount(DummyCmprsRep("RX", 1), 20)] |
504 | 572 |
|
505 | 573 | def test_tracking_name(self): |
506 | 574 | """Test that correct tracking name is returned.""" |
|
0 commit comments