braket.ocean_plugin.braket_sampler module

class braket.ocean_plugin.braket_sampler.BraketSampler(s3_destination_folder: Optional[braket.aws.aws_session.S3DestinationFolder] = None, device_arn: Optional[str] = None, aws_session: Optional[braket.aws.aws_session.AwsSession] = None, logger: logging.Logger = <Logger braket.ocean_plugin.braket_sampler (WARNING)>)[source]

Bases: dimod.core.sampler.Sampler, dimod.core.structured.Structured

A class for using Amazon Braket as a sampler

Parameters:
  • s3_destination_folder (AwsSession.S3DestinationFolder) – NamedTuple with bucket (index 0) and key (index 1) that is the results destination folder in S3.
  • device_arn (str) – AWS quantum device arn.
  • aws_session (AwsSession) – AwsSession to call AWS with.
  • logger (Logger) – Python Logger object with which to write logs, such as QuantumTask statuses while polling for task to complete. Default is getLogger(__name__)

Examples

>>> from braket.ocean_plugin import BraketSampler
>>> s3_destination_folder = ('test_bucket', 'test_folder')
>>> sampler = BraketSampler(s3_destination_folder, "device_arn_1")
properties

Solver properties in Braket boto3 response format

Please see braket.device_schema in amazon-braket-schemas-python for the key names of properties for a device.

Solver properties are dependent on the selected solver and subject to change; for example, new released features may add properties.

Type:FrozenDict[str, Any]
parameters

Solver parameters in the form of a dict, where keys are keyword parameters in Braket format and values are lists of properties in BraketSampler.properties for each key.

Please see braket.device_schema in amazon-braket-schemas-python for the key names of parameters for a device.

Solver parameters are dependent on the selected solver and subject to change; for example, new released features may add parameters.

Type:FrozenDict[str, List]
nodelist

Tuple of active qubits for the solver.

Type:Tuple[int]
edgelist

Tuple of active couplers for the solver.

Type:Tuple[Tuple[int, int]]
sample_ising(h: Union[Dict[int, float], List[float]], J: Dict[Tuple[int, int], float], **kwargs) → dimod.sampleset.SampleSet[source]

Sample from the specified Ising model.

Parameters:
  • h (dict/list) – Linear biases of the Ising model. If a dict, should be of the form {v: bias, ...} where v is a spin-valued variable and bias is its associated bias. If a list, it is treated as a list of biases where the indices are the variable labels, except in the case of missing qubits in which case 0 biases are ignored while a non-zero bias set on a missing qubit raises an error.
  • J (dict[(int, int) – float]): Quadratic biases of the Ising model.
  • **kwargs – Optional keyword arguments for the sampling method in Braket boto3 format
Returns:

dimod.SampleSet – A dimod SampleSet object.

Raises:
  • BinaryQuadraticModelStructureError – If problem graph is incompatible with solver
  • ValueError – If keyword argument is unsupported by solver

Examples

This example submits a two-variable Ising problem mapped directly to qubits 0 and 1 on a D-Wave 2000Q device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/DW_2000Q_6"
>>> sampler = BraketSampler(device_arn_1)
>>> h = {0: -1, 1: 1}
>>> sampleset = sampler.sample_ising(h, {}, resultFormat="HISTOGRAM")
>>> for sample in sampleset.samples():
...    print(sample)
...
{0: 1, 1: -1}

This example submits a two-variable Ising problem mapped directly to qubits 0 and 1 on a D-Wave Advantage4 device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/Advantage_system4"
>>> sampler = BraketSampler(device_arn_1)
>>> h = {30: -1, 31: 1}
>>> sampleset = sampler.sample_ising(h, {}, resultFormat="HISTOGRAM")
>>> for sample in sampleset.samples():
...    print(sample)
...
{30: 1, 31: -1}
sample_ising_quantum_task(h: Union[Dict[int, float], List[float]], J: Dict[Tuple[int, int], float], **kwargs) → braket.tasks.quantum_task.QuantumTask[source]

Sample from the specified Ising model and return a QuantumTask. This has the same inputs as BraketSampler.sample_ising.

Parameters:
  • h (dict/list) – Linear biases of the Ising model. If a dict, should be of the form {v: bias, ...} where v is a spin-valued variable and bias is its associated bias. If a list, it is treated as a list of biases where the indices are the variable labels, except in the case of missing qubits in which case 0 biases are ignored while a non-zero bias set on a missing qubit raises an error.
  • J (dict[(int, int) – float]): Quadratic biases of the Ising model.
  • **kwargs – Optional keyword arguments for the sampling method in Braket boto3 format
Returns:

dimod.SampleSet – A dimod SampleSet object.

Raises:
  • BinaryQuadraticModelStructureError – If problem graph is incompatible with solver
  • ValueError – If keyword argument is unsupported by solver

Examples

This example submits a two-variable Ising problem mapped directly to qubits 0 and 1 on a D-Wave 2000Q device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/DW_2000Q_6"
>>> sampler = BraketSampler(device_arn_1)
>>> Q = {0: 1, 1: 1}
>>> task = sampler.sample_ising_quantum_task(Q, {}, resultFormat="HISTOGRAM")
>>> sampleset = BraketSampler.get_task_sample_set(task)
>>> for sample in sampleset.samples():
...    print(sample)
...
{0: 1, 1: -1}

This example submits a two-variable Ising problem mapped directly to qubits 0 and 1 on a D-Wave Advantage4 device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/Advantage_system4"
>>> sampler = BraketSampler(device_arn_1)
>>> Q = {30: 1, 31: 1}
>>> task = sampler.sample_ising_quantum_task(Q, {}, resultFormat="HISTOGRAM")
>>> sampleset = BraketSampler.get_task_sample_set(task)
>>> for sample in sampleset.samples():
...    print(sample)
...
{30: 1, 31: -1}
sample_qubo(Q: Dict[Tuple[int, int], float], **kwargs) → dimod.sampleset.SampleSet[source]

Sample from the specified QUBO.

Parameters:
  • Q (dict) – Coefficients of a quadratic unconstrained binary optimization (QUBO) model.
  • **kwargs – Optional keyword arguments for the sampling method in Braket boto3 format
Returns:

dimod.SampleSet – A dimod SampleSet object.

Raises:
  • BinaryQuadraticModelStructureError – If problem graph is incompatible with solver
  • ValueError – If keyword argument is unsupported by solver

Examples

This example submits a two-variable QUBO mapped directly to qubits 0 and 4 on a sampler on the D-Wave 2000Q device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/DW_2000Q_6"
>>> sampler = BraketSampler(device_arn_1)
>>> Q = {(0, 0): -1, (4, 4): -1, (0, 4): 2}
>>> sampleset = sampler.sample_qubo(Q, postprocessingType="SAMPLING", shots=100)
>>> for sample in sampleset.samples():
...    print(sample)
...
{0: 0, 4: 1}
{0: 1, 4: 0}

This example submits a two-variable QUBO mapped directly to qubits 30 and 31 on a sampler on the D-Wave Advantage4 device. >>> from braket.ocean_plugin import BraketSampler >>> device_arn_1 = “arn:aws:braket:::device/qpu/d-wave/Advantage_system4” >>> sampler = BraketSampler(device_arn_1) >>> Q = {(30, 30): -1, (31, 31): -1, (30, 31): 2} >>> sampleset = sampler.sample_qubo(Q, shots=100) >>> for sample in sampleset.samples(): … print(sample) … {30: 0, 31: 1} {30: 1, 31: 0}

sample_qubo_quantum_task(Q: Dict[Tuple[int, int], float], **kwargs) → braket.tasks.quantum_task.QuantumTask[source]

Sample from the specified QUBO and return a QuantumTask. This has the same inputs as BraketSampler.sample_qubo

Parameters:
  • Q (dict) – Coefficients of a quadratic unconstrained binary optimization (QUBO) model.
  • **kwargs – Optional keyword arguments for the sampling method in Braket boto3 format
Returns:

dimod.SampleSet – A dimod SampleSet object.

Raises:
  • BinaryQuadraticModelStructureError – If problem graph is incompatible with solver
  • ValueError – If keyword argument is unsupported by solver

Examples

This example submits a two-variable QUBO mapped directly to qubits 0 and 4 on a sampler on the D-Wave 2000Q device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/DW_2000Q_6"
>>> sampler = BraketSampler(device_arn_1)
>>> Q = {(0, 0): -1, (4, 4): -1, (0, 4): 2}
>>> task = sampler.sample_qubo_quantum_task(Q, resultFormat="HISTOGRAM", shots=100)
>>> sampleset = BraketSampler.get_task_sample_set(task)
>>> for sample in sampleset.samples():
...    print(sample)
...
{0: 0, 4: 1}
{0: 1, 4: 0}

This example submits a two-variable QUBO mapped directly to qubits 30 and 31 on a sampler on the D-Wave Advantage4 device.

>>> from braket.ocean_plugin import BraketSampler
>>> device_arn_1 = "arn:aws:braket:::device/qpu/d-wave/Advantage_system4"
>>> sampler = BraketSampler(device_arn_1)
>>> Q = {(30, 30): -1, (31, 31): -1, (30, 31): 2}
>>> task = sampler.sample_qubo_quantum_task(Q, resultFormat="HISTOGRAM", shots=100)
>>> sampleset = BraketSampler.get_task_sample_set(task)
>>> for sample in sampleset.samples():
...    print(sample)
...
{30: 0, 31: 1}
{30: 1, 31: 0}
static get_task_sample_set(task: braket.tasks.quantum_task.QuantumTask, variables: Optional[Set[int]] = None) → dimod.sampleset.SampleSet[source]

Get SampleSet from an QuantumTask object

Parameters:
  • task (QuantumTask) – task from which to get SampleSet
  • variables (Set[int], optional) – variables for samples in SampleSet. The default is the set of active variables for D-Wave. If there are no active variables marked as part of the task result, the default is list(range(result.variable_count)).
Returns:

dimod.SampleSet – A dimod SampleSet object.

Examples

>>> from braket.ocean_plugin import BraketSampler
>>> from braket.aws import AwsQuantumTask
>>> sample_set = sampler.get_task_sample_set(AwsQuantumTask(arn="your_arn"))