Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run_subcircuits_using_sampler function #72

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from qiskit import QuantumCircuit
from qiskit.converters import circuit_to_dag, dag_to_circuit
from qiskit.circuit.library.standard_gates import HGate, SGate, SdgGate, XGate
from qiskit.primitives import Sampler as TestSampler
from qiskit.primitives import BaseSampler, Sampler as TestSampler
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Session, Options


Expand Down Expand Up @@ -204,20 +204,16 @@ def modify_subcircuit_instance(
return subcircuit_instance_circuit


def run_subcircuits(
def run_subcircuits_using_sampler(
subcircuits: Sequence[QuantumCircuit],
service: Optional[QiskitRuntimeService] = None,
backend_name: Optional[str] = None,
options: Optional[Options] = None,
sampler: BaseSampler,
) -> List[NDArray]:
"""
Execute the subcircuit(s).

Args:
- subcircuit (QuantumCircuit): the subcircuits to be executed
- service (QiskitRuntimeService): the runtime service
- backend_name (str): the backend used to execute the subcircuits
- options (Options): options for the runtime execution of subcircuits
- sampler (BaseSampler): the Sampler to use for executions

Returns:
- (NDArray): the probability distributions
Expand All @@ -226,12 +222,6 @@ def run_subcircuits(
if subcircuit.num_clbits == 0:
subcircuit.measure_all()

if service is not None:
session = Session(service=service, backend=backend_name)
sampler = Sampler(session=session, options=options)
else:
sampler = TestSampler(options=options)

quasi_dists = sampler.run(circuits=subcircuits).result().quasi_dists

all_probabilities_out = []
Expand All @@ -246,6 +236,33 @@ def run_subcircuits(
return all_probabilities_out


def run_subcircuits(
subcircuits: Sequence[QuantumCircuit],
service: Optional[QiskitRuntimeService] = None,
backend_name: Optional[str] = None,
options: Optional[Options] = None,
) -> List[NDArray]:
"""
Execute the subcircuit(s).

Args:
- subcircuit (QuantumCircuit): the subcircuits to be executed
- service (QiskitRuntimeService): the runtime service
- backend_name (str): the backend used to execute the subcircuits
- options (Options): options for the runtime execution of subcircuits

Returns:
- (NDArray): the probability distributions
"""
if service is not None:
session = Session(service=service, backend=backend_name)
sampler = Sampler(session=session, options=options)
else:
sampler = TestSampler(options=options)

return run_subcircuits_using_sampler(subcircuits, sampler)


def measure_prob(unmeasured_prob: NDArray, meas: Tuple[Any, ...]) -> NDArray:
"""
Compute the effective probability distribution from the subcircuit distribution.
Expand Down