()
| 77 | |
| 78 | |
| 79 | def test_sum_conversions(): |
| 80 | params = {"squeezing": 0.5, "displacement": 0.25} |
| 81 | dims = {0: 2, 1: 2} |
| 82 | matrix_product = operators.squeeze(0) * operators.displace(1) |
| 83 | matrix_product_expected = np.kron(displace_matrix(2, 0.25), |
| 84 | squeeze_matrix(2, 0.5)) |
| 85 | spin_product = spin.y(1) * spin.x(0) |
| 86 | spin_product_expected = np.kron(pauliy_matrix(), paulix_matrix()) |
| 87 | boson_product = boson.annihilate(1) * boson.number(0) |
| 88 | boson_product_expected = np.kron(annihilate_matrix(2), number_matrix(2)) |
| 89 | # Combining fermion operators on the same targets with other operators |
| 90 | # generally should result in an error being raised for conflicting commutation |
| 91 | # relations. However, this error is only raised upon product creation, not |
| 92 | # necessarily when creating the sum. (FIXME?) |
| 93 | fermion_product = fermion.annihilate(0) * fermion.create(1) |
| 94 | fermion_product_expected = np.kron(create_matrix(2), annihilate_matrix(2)) |
| 95 | |
| 96 | product_ops = [(matrix_product, matrix_product_expected), |
| 97 | (spin_product, spin_product_expected), |
| 98 | (boson_product, boson_product_expected), |
| 99 | (fermion_product, fermion_product_expected)] |
| 100 | |
| 101 | matrix_sum = operators.squeeze(0) + operators.displace(1) |
| 102 | matrix_sum_expected = np.kron(displace_matrix(2, 0.25), identity_matrix(2)) +\ |
| 103 | np.kron(identity_matrix(2), squeeze_matrix(2, 0.5)) |
| 104 | spin_sum = spin.y(1) + spin.x(0) |
| 105 | spin_sum_expected = np.kron(pauliy_matrix(), identity_matrix(2)) +\ |
| 106 | np.kron(identity_matrix(2), paulix_matrix()) |
| 107 | boson_sum = boson.annihilate(1) + boson.number(0) |
| 108 | boson_sum_expected = np.kron(annihilate_matrix(2), identity_matrix(2)) +\ |
| 109 | np.kron(identity_matrix(2), number_matrix(2)) |
| 110 | fermion_sum = fermion.annihilate(0) + fermion.create(1) |
| 111 | fermion_sum_expected = np.kron(create_matrix(2), identity_matrix(2)) +\ |
| 112 | np.kron(identity_matrix(2), annihilate_matrix(2)) |
| 113 | |
| 114 | sum_ops = [(matrix_sum, matrix_sum_expected), (spin_sum, spin_sum_expected), |
| 115 | (boson_sum, boson_sum_expected), |
| 116 | (fermion_sum, fermion_sum_expected)] |
| 117 | |
| 118 | sum_type = dict([(operators.MatrixOperatorTerm, operators.MatrixOperator), |
| 119 | (spin.SpinOperatorTerm, spin.SpinOperator), |
| 120 | (boson.BosonOperatorTerm, boson.BosonOperator), |
| 121 | (fermion.FermionOperatorTerm, fermion.FermionOperator)]) |
| 122 | |
| 123 | def check_equals(op, expected, expected_type): |
| 124 | assert type(op) == expected_type |
| 125 | assert np.allclose(op.to_matrix(dims, params), expected) |
| 126 | |
| 127 | for (sum1, expected1) in sum_ops: |
| 128 | for (sum2, expected2) in sum_ops: |
| 129 | print("check arithmetics for {t1} with {t2}".format(t1=type(sum1), |
| 130 | t2=type(sum2))) |
| 131 | expected_type = type(sum1) |
| 132 | if type(sum1) != type(sum2): |
| 133 | expected_type = operators.MatrixOperator |
| 134 | check_equals(sum1 + sum2, expected1 + expected2, expected_type) |
| 135 | check_equals(sum1 - sum2, expected1 - expected2, expected_type) |
| 136 | if isinstance(sum1, fermion.FermionOperator) != isinstance( |
nothing calls this directly
no test coverage detected