This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 0.2.1802.2202 - netcore & python preview (#32)
* Switching repository to MIT License * Release 0.2.1802.2202 - netcore & mit license * Added license headers to H2 simulation sample.
- Loading branch information
Showing
106 changed files
with
4,213 additions
and
3,026 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
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,19 @@ | ||
## | ||
# environment.yml: Specification of a conda environment for use with | ||
# the Q# interoperability package. | ||
## | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
## | ||
|
||
name: qsharp | ||
channels: | ||
- conda-forge | ||
- pythonnet | ||
dependencies: | ||
- numpy | ||
- pythonnet | ||
- qutip | ||
- matplotlib | ||
- pip: | ||
- qinfer |
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,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.Quantum.Development.Kit" version="0.2.1802.2202-preview" targetFramework="netstandard2.0" /> | ||
<package id="Microsoft.Quantum.Canon" version="0.2.1802.2202-preview" targetFramework="netstandard2.0" /> | ||
</packages> |
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,230 @@ | ||
#!/bin/env python | ||
# -*- coding: utf-8 -*- | ||
## | ||
# __init__.py: Root module for Q# interoperability package. | ||
## | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
## | ||
|
||
## IMPORTS ################################################################### | ||
|
||
# Standard Library Imports # | ||
|
||
from enum import IntEnum | ||
from functools import partial, singledispatch | ||
from abc import ABCMeta, abstractproperty | ||
from random import randrange | ||
|
||
# HACK: In order to make DLLs that we ship with visible to Python.NET | ||
# in the next step, we need to add the directory containing __file__ | ||
# to sys.path. | ||
import os.path | ||
import sys | ||
sys.path.append(os.path.split(__file__)[0]) | ||
|
||
# Version Information # | ||
# We try to import version.py, and if not, set our version to None. | ||
try: | ||
import qsharp.version | ||
__version__ = qsharp.version.version | ||
except ImportError: | ||
__version__ = "<unknown>" | ||
|
||
# CLR Imports # | ||
|
||
import clr | ||
|
||
import System.Threading.Tasks | ||
import Microsoft.Quantum.Simulation.Simulators as mqss | ||
|
||
# External Python Imports # | ||
|
||
try: | ||
import colorama as ca | ||
ca.init() | ||
|
||
SIMULATOR_COLOR = ca.Fore.BLUE + ca.Back.WHITE | ||
RESET_COLOR = ca.Style.RESET_ALL | ||
except: | ||
ca = None | ||
SIMULATOR_COLOR = "" | ||
RESET_COLOR = "" | ||
|
||
try: | ||
import qutip as qt | ||
except: | ||
qt = None | ||
|
||
try: | ||
from IPython.display import display | ||
except: | ||
def display(value): | ||
pass | ||
|
||
import qsharp.tomography | ||
from qsharp.clr_wrapper import * | ||
|
||
|
||
## ENUMERATIONS ############################################################## | ||
|
||
class SampleableEnum(IntEnum): | ||
""" | ||
Extends integer-valued enumerations to allow for uniformly sampling from | ||
enumeration members. | ||
""" | ||
@classmethod | ||
def random(cls): | ||
""" | ||
Returns a member of the enumeration uniformly at random. | ||
""" | ||
return cls(randrange(len(cls))) | ||
|
||
class Pauli(SampleableEnum): | ||
""" | ||
Represents the `Pauli` Q# type. | ||
""" | ||
I = 0 | ||
X = 1 | ||
Y = 2 | ||
Z = 3 | ||
|
||
def as_qobj(self): | ||
""" | ||
Returns a representation of the given Pauli operator as a QuTiP | ||
Qobj instance. | ||
""" | ||
if qt is None: | ||
raise RuntimeError("Requires QuTiP.") | ||
if self == Pauli.I: | ||
return qt.qeye(2) | ||
elif self == Pauli.X: | ||
return qt.sigmax() | ||
elif self == Pauli.Y: | ||
return qt.sigmay() | ||
else: | ||
return qt.sigmaz() | ||
|
||
class Result(SampleableEnum): | ||
""" | ||
Represents the `Result` Q# type. | ||
""" | ||
|
||
#: Represents a measurement corresponding to the $+1 = (-1)^0$ eigenvalue | ||
#: of a Pauli operator. | ||
Zero = 0 | ||
|
||
#: Represents a measurement corresponding to the $-1 = (-1)^1$ eigenvalue | ||
#: of a Pauli operator. | ||
One = 1 | ||
|
||
## FUNCTIONS ################################################################# | ||
|
||
@singledispatch | ||
def wrap_clr_object(clr_object): | ||
return WrappedCLRObject(clr_object) | ||
|
||
@wrap_clr_object.register(type(None)) | ||
def _(none): | ||
return None | ||
|
||
@wrap_clr_object.register(System.Threading.Tasks.Task) | ||
def _(clr_object): | ||
return Task(clr_object) | ||
|
||
## CLASSES ################################################################### | ||
|
||
class SimulatorOutput(object): | ||
""" | ||
Wraps a string for pretty-printing to Jupyter outputs. | ||
""" | ||
@classmethod | ||
def display_output(cls, data): | ||
display(cls(data)) | ||
|
||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __repr__(self): | ||
return repr(self.data) | ||
def __str__(self): | ||
return "{}[Simulator]{} {}".format( | ||
SIMULATOR_COLOR, RESET_COLOR, self.data | ||
) | ||
|
||
def _repr_html_(self): | ||
return """ | ||
<pre class="simulator-output">{}</pre> | ||
""".format(self.data) | ||
|
||
class Task(WrappedCLRObject): | ||
_detailed_repr = False | ||
|
||
@property | ||
def _friendly_name(self): | ||
return "Asynchronous {} Task".format( | ||
self.clr_type.GetGenericArguments()[0] | ||
) | ||
|
||
def result(self): | ||
# Wait for the response first. This should be implied by self.Result, | ||
# but we've seen some bugs occur related to that. | ||
self.clr_object.Wait() | ||
return wrap_clr_object(self.clr_object.Result) | ||
|
||
class Simulator(WrappedCLRObject): | ||
_friendly_name = "Simulator" | ||
|
||
def __init__(self, clr_simulator): | ||
if isinstance(clr_simulator, CLR_METATYPE): | ||
clr_simulator = clr_simulator() | ||
super(Simulator, self).__init__(clr_simulator) | ||
|
||
# Register a delegate to handle Message calls. | ||
self.clr_object.OnLog += SimulatorOutput.display_output | ||
|
||
def get(self, clr_type): | ||
return Callable(self, self.clr_object.Get[clr_type, clr_type]()) | ||
|
||
def run(self, clr_type, *args): | ||
if isinstance(clr_type, Callable): | ||
# Check if the passed in type is already wrapped in a Callable Python | ||
# class. | ||
return clr_type(*args) | ||
else: | ||
# We need to get the callable ourselves, then. | ||
callable = self.get(clr_type) | ||
return callable(*args) | ||
|
||
class QuantumSimulator(Simulator): | ||
_friendly_name = "Quantum Simulator" | ||
def __init__(self): | ||
super(QuantumSimulator, self).__init__(mqss.QuantumSimulator) | ||
|
||
class Callable(WrappedCLRObject): | ||
_detailed_repr = False | ||
_parent = None | ||
_include_plain_repr = False | ||
|
||
@property | ||
def _friendly_name(self): | ||
# TODO: extract operation signature! | ||
return self.ToString() | ||
|
||
def __init__(self, parent, clr_callable): | ||
self._parent = parent | ||
super(Callable, self).__init__(clr_callable) | ||
|
||
def __call__(self, *args): | ||
output = self.clr_object.Run( | ||
unwrap_clr(self._parent), | ||
*map(unwrap_clr, args) | ||
) | ||
|
||
if isinstance(type(output), CLR_METATYPE): | ||
# Try to wrap the output as best as we can. | ||
# We provide convienence wrappers for a few, so we call the | ||
# single-dispatched convienence function above. | ||
return wrap_clr_object(output) | ||
else: | ||
return output |
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,19 @@ | ||
#!/bin/env python | ||
# -*- coding: utf-8 -*- | ||
## | ||
# __main__.py: Main script for the Q# interoperability package. | ||
## | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
## | ||
|
||
import qsharp | ||
|
||
from qsharp.tomography import single_qubit_process_tomography | ||
from Microsoft.Quantum.Primitive import I | ||
|
||
qsim = qsharp.QuantumSimulator() | ||
noise_channel = qsim.get(I) | ||
estimation_results = single_qubit_process_tomography(qsim, noise_channel, n_measurements=2000) | ||
|
||
print(estimation_results['est_channel']) |
Oops, something went wrong.