-
Notifications
You must be signed in to change notification settings - Fork 19
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
Feature/2q rb #239
Open
deanpoulos
wants to merge
15
commits into
main
Choose a base branch
from
feature/2q_rb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/2q rb #239
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ecaf13b
Migrate 2-qubit randomized benchmarking from qua-libs.
deanpoulos 278ea81
Add graceful handling of cirq dependency.
deanpoulos 35f8dbb
Update changelog and readme.
deanpoulos ee0f189
Fix formatting.
deanpoulos 1f5d2a1
Fix formatting.
deanpoulos ac63862
Pin cirq version in IMportErrror message.
deanpoulos 120ea96
Fix TwoQubitRbDebugger issues and documentation.
deanpoulos 71d640f
Add bell-state construction circuit.
deanpoulos 0c22cf8
Revert input streaming behaviour.
deanpoulos 0f75a8b
Attempt fix SWAP implementation.
deanpoulos 83c18e3
Merge with main.
deanpoulos 14a0f2c
Remove tqdm import.
deanpoulos 2c94dc6
Get cirq working as an extra dependency with two-qubit-rb when python…
deanpoulos 06b57a1
Merge with main.
deanpoulos 745269e
Update workflow file to test other optinal dependencies.
deanpoulos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,7 @@ | ||
# Characterization | ||
The `characterization` module includes standard implementations for complex characterization protocols. | ||
|
||
## Sub-modules | ||
It includes: | ||
|
||
* [Two Qubit RB](two_qubit_rb/README.md) - This library includes an efficient implementation of 2-qubit Randomized Benchmarking, enabling measurement of the 2-qubit Clifford gate fidelity and interleaved gate fidelity. |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
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,3 @@ | ||
from .two_qubit_rb import * | ||
|
||
__all__ = ["TwoQubitRb", "TwoQubitRbDebugger"] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 changes: 165 additions & 0 deletions
165
qualang_tools/characterization/two_qubit_rb/two_qubit_rb/RBBaker.py
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,165 @@ | ||
from ._cirq import import_cirq | ||
from .gates import GateGenerator, gate_db | ||
from .verification.command_registry import CommandRegistry | ||
|
||
import copy | ||
import json | ||
from typing import Callable, Dict, Optional, List | ||
|
||
from cirq import GateOperation | ||
from qm.qua import switch_, case_, declare, align, for_ | ||
from qualang_tools.bakery.bakery import Baking, baking | ||
from tqdm import tqdm | ||
|
||
cirq = import_cirq() | ||
|
||
|
||
class RBBaker: | ||
def __init__( | ||
self, | ||
config, | ||
single_qubit_gate_generator: Callable, | ||
two_qubit_gate_generators: Dict[str, Callable], | ||
interleaving_gate: Optional[List[cirq.GateOperation]] = None, | ||
command_registry: Optional[CommandRegistry] = None, | ||
): | ||
self._command_registry = command_registry | ||
self._config = copy.deepcopy(config) | ||
self._single_qubit_gate_generator = single_qubit_gate_generator | ||
self._two_qubit_gate_generators = two_qubit_gate_generators | ||
self._interleaving_gate = interleaving_gate | ||
self._symplectic_generator = GateGenerator(set(two_qubit_gate_generators.keys())) | ||
self._all_elements = self._collect_all_elements() | ||
self._cmd_to_op = {} | ||
self._op_to_baking = {} | ||
|
||
@property | ||
def all_elements(self): | ||
return self._all_elements | ||
|
||
@staticmethod | ||
def _get_qubits(op): | ||
return [q.x for q in op.qubits] | ||
|
||
@staticmethod | ||
def _get_phased_xz_args(op): | ||
return op.gate.x_exponent, op.gate.z_exponent, op.gate.axis_phase_exponent | ||
|
||
def _validate_two_qubit_gate_available(self, name): | ||
if name not in self._two_qubit_gate_generators: | ||
raise RuntimeError(f"Two qubit gate '{name}' implementation not provided.") | ||
|
||
def _gen_gate(self, baker: Baking, gate_op: GateOperation): | ||
if isinstance(gate_op.gate, cirq.PhasedXZGate): | ||
self._single_qubit_gate_generator(baker, self._get_qubits(gate_op)[0], *self._get_phased_xz_args(gate_op)) | ||
elif isinstance(gate_op.gate, cirq.ISwapPowGate) and gate_op.gate.exponent == 0.5: | ||
self._validate_two_qubit_gate_available("sqr_iSWAP") | ||
self._two_qubit_gate_generators["sqr_iSWAP"](baker, *self._get_qubits(gate_op)) | ||
elif isinstance(gate_op.gate, cirq.CNotPowGate) and gate_op.gate.exponent == 1: | ||
self._validate_two_qubit_gate_available("CNOT") | ||
self._two_qubit_gate_generators["CNOT"](baker, *self._get_qubits(gate_op)) | ||
elif isinstance(gate_op.gate, cirq.CZPowGate) and gate_op.gate.exponent == 1: | ||
self._validate_two_qubit_gate_available("CZ") | ||
self._two_qubit_gate_generators["CZ"](baker, *self._get_qubits(gate_op)) | ||
else: | ||
raise RuntimeError("unsupported gate") | ||
|
||
def _collect_all_elements(self): | ||
config = copy.deepcopy(self._config) | ||
qes = set() | ||
for cmd_id, command in enumerate(gate_db.commands): | ||
if self._command_registry is not None: | ||
self._command_registry.set_current_command_id(cmd_id) | ||
with baking(config) as b: | ||
self._update_baking_from_cmd_id(b, cmd_id) | ||
qes.update(b.get_qe_set()) | ||
b.update_config = False | ||
if self._interleaving_gate is not None: | ||
if self._command_registry is not None: | ||
self._command_registry.set_current_command_id(cmd_id + 1) | ||
with baking(config) as b: | ||
self._update_baking_from_gates(b, self._interleaving_gate) | ||
qes.update(b.get_qe_set()) | ||
b.update_config = False | ||
if self._command_registry is not None: | ||
self._command_registry.finish() | ||
return qes | ||
|
||
def _update_baking_from_gates(self, b: Baking, gate_ops, elements=None): | ||
prev_gate_qubits = [] | ||
for gate_op in gate_ops: | ||
gate_qubits = self._get_qubits(gate_op) | ||
if len(gate_qubits) != len(prev_gate_qubits) and elements is not None: | ||
b.align(*elements) | ||
prev_gate_qubits = gate_qubits | ||
self._gen_gate(b, gate_op) | ||
if elements is not None: | ||
b.align(*elements) | ||
|
||
def gates_from_cmd_id(self, cmd_id): | ||
if 0 <= cmd_id < len(gate_db.commands): | ||
gate_ops = self._symplectic_generator.generate(cmd_id) | ||
elif self._interleaving_gate is not None and cmd_id == len(gate_db.commands): # Interleaving gate | ||
gate_ops = self._interleaving_gate | ||
else: | ||
raise RuntimeError("command out of range") | ||
return gate_ops | ||
|
||
def _update_baking_from_cmd_id(self, b: Baking, cmd_id, elements=None): | ||
gate_ops = self.gates_from_cmd_id(cmd_id) | ||
return self._update_baking_from_gates(b, gate_ops, elements) | ||
|
||
@staticmethod | ||
def _unique_baker_identifier_for_qe(b: Baking, qe: str): | ||
identifier = {"samples": b._samples_dict[qe], "info": b._qe_dict[qe]} | ||
return json.dumps(identifier) | ||
|
||
def _bake_all_ops(self, config: dict): | ||
waveform_id_per_qe = {qe: 0 for qe in self._all_elements} | ||
waveform_to_baking = {qe: {} for qe in self._all_elements} | ||
cmd_to_op = {qe: {} for qe in self._all_elements} | ||
op_to_baking = {qe: [] for qe in self._all_elements} | ||
num_of_commands = len(gate_db.commands) + (0 if self._interleaving_gate is None else 1) | ||
for cmd_id in tqdm(range(num_of_commands), desc="Baking pulses which comprise Cliffords", unit="command"): | ||
with baking(config) as b: | ||
self._update_baking_from_cmd_id(b, cmd_id, self._all_elements) | ||
any_qe_used = False | ||
for qe in self._all_elements: | ||
key = self._unique_baker_identifier_for_qe(b, qe) | ||
if key not in waveform_to_baking[qe]: | ||
waveform_to_baking[qe][key] = waveform_id_per_qe[qe], b | ||
op_to_baking[qe].append(b) | ||
waveform_id_per_qe[qe] += 1 | ||
any_qe_used = True | ||
cmd_to_op[qe][cmd_id] = waveform_to_baking[qe][key][0] | ||
b.update_config = any_qe_used | ||
return cmd_to_op, op_to_baking | ||
|
||
def bake(self) -> dict: | ||
config = copy.deepcopy(self._config) | ||
self._cmd_to_op, self._op_to_baking = self._bake_all_ops(config) | ||
return config | ||
|
||
def decode(self, cmd_id, element): | ||
return self._cmd_to_op[element][cmd_id] | ||
|
||
@staticmethod | ||
def _run_baking_for_qe(b: Baking, qe: str): | ||
orig_get_qe_set = b.get_qe_set | ||
b.get_qe_set = lambda: {qe} | ||
b.run() | ||
b.get_qe_set = orig_get_qe_set | ||
|
||
def run(self, op_list_per_qe: dict, length, unsafe=True): | ||
if set(op_list_per_qe.keys()) != self._all_elements: | ||
raise RuntimeError(f"must specify ops for all elements: {', '.join(self._all_elements)} ") | ||
|
||
align() | ||
for qe, op_list in op_list_per_qe.items(): | ||
cmd_i = declare(int) | ||
with for_(cmd_i, 0, cmd_i < length, cmd_i + 1): | ||
with switch_(op_list[cmd_i], unsafe=unsafe): | ||
for op_id, b in enumerate(self._op_to_baking[qe]): | ||
with case_(op_id): | ||
self._run_baking_for_qe(b, qe) | ||
align() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you go ahead and update all of the optional dependencies? many are missing, and config builder is deprecated.
We can also decide to decide not listing specific ones, and just mention that in some cases it is needed (and mention it in the internal readmes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the Data Handler, which was missing, and added a node that the config builder is deprecated. You said many are missing- did I miss any?