|
17 | 17 | import pytest |
18 | 18 |
|
19 | 19 | from braket.default_simulator import gate_operations, observables |
| 20 | +from braket.default_simulator.linalg_utils import controlled_matrix |
20 | 21 | from braket.default_simulator.state_vector_simulation import StateVectorSimulation |
21 | 22 |
|
22 | 23 | evolve_testdata = [ |
@@ -544,3 +545,85 @@ def test_ghz_tree_retrieve_samples(batch_size): |
544 | 545 | assert 0.4 < counter[0] / (counter[0] + counter[960]) < 0.6 |
545 | 546 | assert 0.4 < counter[960] / (counter[0] + counter[960]) < 0.6 |
546 | 547 | assert counter[0] + counter[960] == 10000 |
| 548 | + |
| 549 | + |
| 550 | +_POWERS = [0, 1, 2, 3, -1, -2, 0.5, -0.5, 1.5] |
| 551 | + |
| 552 | +_POWERED_GATES = [ |
| 553 | + (gate_operations.Hadamard, 1, ()), |
| 554 | + (gate_operations.PauliX, 1, ()), |
| 555 | + (gate_operations.PauliZ, 1, ()), |
| 556 | + (gate_operations.S, 1, ()), |
| 557 | + (gate_operations.T, 1, ()), |
| 558 | + (gate_operations.RotX, 1, (0.37,)), |
| 559 | + (gate_operations.RotZ, 1, (0.37,)), |
| 560 | + (gate_operations.PhaseShift, 1, (0.37,)), |
| 561 | + (gate_operations.CX, 2, ()), |
| 562 | + (gate_operations.CY, 2, ()), |
| 563 | + (gate_operations.CZ, 2, ()), |
| 564 | + (gate_operations.Swap, 2, ()), |
| 565 | + (gate_operations.ISwap, 2, ()), |
| 566 | + (gate_operations.ECR, 2, ()), |
| 567 | + (gate_operations.CPhaseShift, 2, (0.37,)), |
| 568 | + (gate_operations.PSwap, 2, (0.37,)), |
| 569 | + (gate_operations.XX, 2, (0.37,)), |
| 570 | + (gate_operations.ZZ, 2, (0.37,)), |
| 571 | +] |
| 572 | + |
| 573 | + |
| 574 | +def _preparation_operations(qubit_count): |
| 575 | + return [gate_operations.RotY([q], 0.3 + 0.4 * q) for q in range(qubit_count)] + [ |
| 576 | + gate_operations.RotZ([q], 0.2 + 0.3 * q) for q in range(qubit_count) |
| 577 | + ] |
| 578 | + |
| 579 | + |
| 580 | +def _apply_reference(state_vector, operation, qubit_count): |
| 581 | + matrix = controlled_matrix(operation.matrix, operation.control_state) |
| 582 | + targets = operation.targets |
| 583 | + num_targets = len(targets) |
| 584 | + product = np.tensordot( |
| 585 | + matrix.reshape([2] * 2 * num_targets), |
| 586 | + state_vector.reshape([2] * qubit_count), |
| 587 | + axes=(range(num_targets, 2 * num_targets), targets), |
| 588 | + ) |
| 589 | + return np.moveaxis(product, range(num_targets), targets).reshape(-1) |
| 590 | + |
| 591 | + |
| 592 | +def _reference_state_vector(operations, qubit_count): |
| 593 | + state_vector = np.zeros(2**qubit_count, dtype=complex) |
| 594 | + state_vector[0] = 1 |
| 595 | + for operation in operations: |
| 596 | + state_vector = _apply_reference(state_vector, operation, qubit_count) |
| 597 | + return state_vector |
| 598 | + |
| 599 | + |
| 600 | +@pytest.mark.parametrize("qubit_count, targets", [(3, (2, 0)), (11, (9, 3))]) |
| 601 | +@pytest.mark.parametrize("power", _POWERS) |
| 602 | +@pytest.mark.parametrize("gate_class, num_targets, args", _POWERED_GATES) |
| 603 | +def test_powered_gate(gate_class, num_targets, args, power, qubit_count, targets): |
| 604 | + operations = _preparation_operations(qubit_count) + [ |
| 605 | + gate_class(targets[:num_targets], *args, power=power) |
| 606 | + ] |
| 607 | + simulation = StateVectorSimulation(qubit_count, 0, 1) |
| 608 | + simulation.evolve(operations) |
| 609 | + assert np.allclose(simulation.state_vector, _reference_state_vector(operations, qubit_count)) |
| 610 | + |
| 611 | + |
| 612 | +@pytest.mark.parametrize( |
| 613 | + "qubit_count, targets, control_state", |
| 614 | + [ |
| 615 | + (4, (1, 3, 0), (1,)), |
| 616 | + (4, (1, 3, 0), (0,)), |
| 617 | + (4, (1, 3, 0, 2), (1, 1)), |
| 618 | + (4, (3, 1, 2, 0), (0, 0)), |
| 619 | + ], |
| 620 | +) |
| 621 | +@pytest.mark.parametrize("power", [0, 2, 0.5, -1]) |
| 622 | +@pytest.mark.parametrize("gate_class", [gate_operations.CX, gate_operations.Swap]) |
| 623 | +def test_controlled_powered_gate(gate_class, power, qubit_count, targets, control_state): |
| 624 | + operations = _preparation_operations(qubit_count) + [ |
| 625 | + gate_class(targets, ctrl_modifiers=control_state, power=power) |
| 626 | + ] |
| 627 | + simulation = StateVectorSimulation(qubit_count, 0, 1) |
| 628 | + simulation.evolve(operations) |
| 629 | + assert np.allclose(simulation.state_vector, _reference_state_vector(operations, qubit_count)) |
0 commit comments