-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add simulator module with mcm simulator implementation (#17)
- Loading branch information
Showing
20 changed files
with
909 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Changelog | ||
|
||
## v0.1.0 (2024-05-09) | ||
## v0.1.0 (2024-05-22) | ||
|
||
This is the initial pre-release version of AutoQASM. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# 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. | ||
|
||
""" | ||
This module contains a local simulator implementation which can be used to | ||
simulate AutoQASM programs at the full OpenQASM 3.0 scope of functionality, | ||
including programs which contain mid-circuit measurement and classical control flow. | ||
The recommended usage of this simulator is by instantiating the Braket LocalSimulator | ||
object with the "autoqasm" backend: | ||
.. code-block:: python | ||
import autoqasm as aq | ||
from braket.devices import LocalSimulator | ||
@aq.main | ||
def bell_state(): | ||
h(0) | ||
cnot(0, 1) | ||
return measure([0, 1]) | ||
device = LocalSimulator("autoqasm") | ||
result = device.run(bell_state, shots=100).result() | ||
""" | ||
|
||
from .simulator import McmSimulator # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# 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. | ||
|
||
from functools import singledispatch | ||
from typing import Any, Union | ||
|
||
import numpy as np | ||
from braket.default_simulator.openqasm._helpers.casting import convert_bool_array_to_string | ||
from braket.default_simulator.openqasm.parser.openqasm_ast import ( | ||
ArrayLiteral, | ||
BitstringLiteral, | ||
BooleanLiteral, | ||
FloatLiteral, | ||
IntegerLiteral, | ||
) | ||
|
||
LiteralType = Union[BooleanLiteral, IntegerLiteral, FloatLiteral, ArrayLiteral, BitstringLiteral] | ||
|
||
|
||
@singledispatch | ||
def convert_to_output(value: LiteralType) -> Any: | ||
raise NotImplementedError(f"converting {value} to output") | ||
|
||
|
||
@convert_to_output.register(IntegerLiteral) | ||
@convert_to_output.register(FloatLiteral) | ||
@convert_to_output.register(BooleanLiteral) | ||
@convert_to_output.register(BitstringLiteral) | ||
def _(value): | ||
return value.value | ||
|
||
|
||
@convert_to_output.register | ||
def _(value: ArrayLiteral): | ||
if isinstance(value.values[0], BooleanLiteral): | ||
return convert_bool_array_to_string(value) | ||
return np.array([convert_to_output(x) for x in value.values]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# 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 itertools | ||
from collections.abc import Iterable | ||
|
||
import numpy as np | ||
|
||
|
||
def measurement_sample(prob: Iterable[float], target_count: int) -> tuple[int]: | ||
"""Draws a random measurement sample. | ||
Args: | ||
prob (Iterable[float]): Probabilities of each measurement outcome. | ||
Possible measurement outcomes range from 0 to 2**target_count-1, | ||
meaning that len(prob) must be equal to 2**target_count. | ||
target_count (int): Number of bits in the resulting measurement. | ||
Returns: | ||
tuple[int]: Measurement outcomes 0 or 1 for each of the target_count bits. | ||
""" | ||
basis_states = np.array(list(itertools.product([0, 1], repeat=target_count))) | ||
outcome_idx = np.random.choice(list(range(2**target_count)), p=prob) | ||
return tuple(basis_states[outcome_idx]) | ||
|
||
|
||
def measurement_collapse_sv( | ||
state_vector: np.ndarray, targets: Iterable[int], outcome: np.ndarray | ||
) -> np.ndarray: | ||
"""Collapses the state vector according to the given measurement outcome. | ||
Args: | ||
state_vector (np.ndarray): The state vector prior to measurement. | ||
targets (Iterable[int]): The qubit indices that were measured. | ||
outcome (np.ndarray): Array of measurement outcomes 0 or 1. | ||
Returns: | ||
np.ndarray: The collapsed state vector after measurement. | ||
""" | ||
qubit_count = int(np.log2(state_vector.size)) | ||
state_tensor = state_vector.reshape([2] * qubit_count) | ||
for qubit, measurement in zip(targets, outcome): | ||
state_tensor[(slice(None),) * qubit + (int(not measurement),)] = 0 | ||
|
||
state_tensor /= np.linalg.norm(state_tensor) | ||
return state_tensor.flatten() |
Oops, something went wrong.