コード例 #1
0
    def _compute_eigenvalues(observables: Tuple[Observable],
                             num_qubits: int) -> np.ndarray:
        if False in [
                isinstance(observable, StandardObservable)
                for observable in observables
        ]:
            # Tensor product of observables contains a mixture
            # of standard and non-standard observables
            eigenvalues = np.array([1])
            for k, g in itertools.groupby(
                    observables, lambda x: isinstance(x, StandardObservable)):
                if k:
                    # Subgroup g contains only standard observables.
                    eigenvalues = np.kron(eigenvalues,
                                          get_pauli_eigenvalues(len(list(g))))
                else:
                    # Subgroup g contains only non-standard observables.
                    for nonstandard in g:
                        # loop through all non-standard observables
                        eigenvalues = np.kron(eigenvalues,
                                              nonstandard.eigenvalues)
        else:
            eigenvalues = get_pauli_eigenvalues(num_qubits=num_qubits)

        eigenvalues.setflags(write=False)
        return eigenvalues
コード例 #2
0
def result_types_tensor_z_z_testing(device: Device, run_kwargs: Dict[str, Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    obs = Observable.Z() @ Observable.Z()
    obs_targets = [0, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots)
    result = device.run(circuit, **run_kwargs).result()

    expected_mean = 0.849694136476246
    expected_var = 0.27801987443788634
    expected_eigs = get_pauli_eigenvalues(1)

    assert_variance_expectation_sample_result(
        result, shots, expected_var, expected_mean, expected_eigs
    )
def result_types_tensor_x_y_testing(device: Device, run_kwargs: Dict[str,
                                                                     Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    obs = Observable.X() @ Observable.Y()
    obs_targets = [0, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs,
                                                   obs_targets, shots)
    result = device.run(circuit, **run_kwargs).result()

    expected_mean = np.sin(theta) * np.sin(phi) * np.sin(varphi)
    expected_var = (8 * np.sin(theta)**2 * np.cos(2 * varphi) * np.sin(phi)**2
                    - np.cos(2 * (theta - phi)) - np.cos(2 * (theta + phi)) +
                    2 * np.cos(2 * theta) + 2 * np.cos(2 * phi) + 14) / 16
    expected_eigs = get_pauli_eigenvalues(1)

    assert_variance_expectation_sample_result(result, shots, expected_var,
                                              expected_mean, expected_eigs)
コード例 #4
0
def result_types_tensor_z_h_y_testing(device: Device, run_kwargs: Dict[str,
                                                                       Any]):
    shots = run_kwargs["shots"]
    theta = 0.432
    phi = 0.123
    varphi = -0.543
    obs = Observable.Z() @ Observable.H() @ Observable.Y()
    obs_targets = [0, 1, 2]
    circuit = get_result_types_three_qubit_circuit(theta, phi, varphi, obs,
                                                   obs_targets, shots)
    tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
    for task in tasks:
        result = device.run(task, **run_kwargs).result()

        expected_mean = -(np.cos(varphi) * np.sin(phi) +
                          np.sin(varphi) * np.cos(theta)) / np.sqrt(2)
        expected_var = (3 + np.cos(2 * phi) * np.cos(varphi)**2 -
                        np.cos(2 * theta) * np.sin(varphi)**2 - 2 *
                        np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)) / 4
        expected_eigs = get_pauli_eigenvalues(1)
        assert_variance_expectation_sample_result(result, shots, expected_var,
                                                  expected_mean, expected_eigs)
コード例 #5
0
def test_get_pauli_eigenvalues_cache_usage(depth):
    """Test that the right number of cachings have been executed after clearing the cache"""
    get_pauli_eigenvalues.cache_clear()
    get_pauli_eigenvalues(depth)
    assert functools._CacheInfo(depth - 1, depth, 128,
                                depth) == get_pauli_eigenvalues.cache_info()
コード例 #6
0
def test_get_pauli_eigenvalues_correct_eigenvalues_three_qubits():
    """Test the get_pauli_eigenvalues function for three qubits"""
    assert np.array_equal(
        get_pauli_eigenvalues(3),
        np.diag(np.kron(z_matrix, np.kron(z_matrix, z_matrix))),
    )
コード例 #7
0
def test_get_pauli_eigenvalues_correct_eigenvalues_one_qubit():
    """Test the get_pauli_eigenvalues function for one qubit"""
    assert np.array_equal(get_pauli_eigenvalues(1), np.diag(z_matrix))
コード例 #8
0
def test_get_pauli_eigenvalues_immutable(num_qubits):
    get_pauli_eigenvalues(num_qubits)[0] = 100
コード例 #9
0
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

import math

import numpy as np
import pytest

from braket.circuits import Gate, Observable
from braket.circuits.observables import observable_from_ir
from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues

testdata = [
    (Observable.I(), Gate.I(), ["i"], (), np.array([1, 1])),
    (Observable.X(), Gate.X(), ["x"], tuple([Gate.H()]),
     get_pauli_eigenvalues(1)),
    (
        Observable.Y(),
        Gate.Y(),
        ["y"],
        tuple([Gate.Z(), Gate.S(), Gate.H()]),
        get_pauli_eigenvalues(1),
    ),
    (Observable.Z(), Gate.Z(), ["z"], (), get_pauli_eigenvalues(1)),
    (Observable.H(), Gate.H(), ["h"], tuple([Gate.Ry(-math.pi / 4)]),
     get_pauli_eigenvalues(1)),
]

invalid_hermitian_matrices = [
    (np.array([[1]])),
    (np.array([1])),
コード例 #10
0
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

import math

import numpy as np
import pytest

from braket.circuits import Gate, Observable
from braket.circuits.observables import observable_from_ir
from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues

testdata = [
    (Observable.I(), Gate.I(), ["i"], (), np.array([1, 1])),
    (Observable.X(), Gate.X(), ["x"], tuple([Gate.H()]), get_pauli_eigenvalues(1)),
    (
        Observable.Y(),
        Gate.Y(),
        ["y"],
        tuple([Gate.Z(), Gate.S(), Gate.H()]),
        get_pauli_eigenvalues(1),
    ),
    (Observable.Z(), Gate.Z(), ["z"], (), get_pauli_eigenvalues(1)),
    (Observable.H(), Gate.H(), ["h"], tuple([Gate.Ry(-math.pi / 4)]), get_pauli_eigenvalues(1)),
]

invalid_hermitian_matrices = [
    (np.array([[1]])),
    (np.array([1])),
    (np.array([0, 1, 2])),
コード例 #11
0
 def eigenvalues(self) -> np.ndarray:
     return get_pauli_eigenvalues(self.qubit_count)