Skip to content

Commit e7e8ef5

Browse files
95-martin-orionrht
authored andcommitted
FSim calibration to Z phase data (quantumlib#5499)
Fixes quantumlib#5397. The documentation aspect of this still needs to be addressed, but the "relevant docs" mentioned in the issue are still being written.
1 parent 30e757a commit e7e8ef5

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

cirq-google/cirq_google/calibration/phased_fsim.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
MutableMapping,
2727
Optional,
2828
Tuple,
29+
Type,
2930
TypeVar,
3031
TYPE_CHECKING,
3132
Generic,
@@ -40,8 +41,15 @@
4041
import cirq
4142
from cirq.experiments.xeb_fitting import XEBPhasedFSimCharacterizationOptions
4243
from cirq_google.api import v2
43-
from cirq_google.engine import Calibration, CalibrationLayer, CalibrationResult, Engine, EngineJob
44-
from cirq_google.ops import FSimGateFamily
44+
from cirq_google.engine import (
45+
Calibration,
46+
CalibrationLayer,
47+
CalibrationResult,
48+
Engine,
49+
EngineJob,
50+
util,
51+
)
52+
from cirq_google.ops import FSimGateFamily, SycamoreGate
4553

4654
if TYPE_CHECKING:
4755
import cirq_google
@@ -50,6 +58,11 @@
5058
_FLOQUET_PHASED_FSIM_HANDLER_NAME = 'floquet_phased_fsim_characterization'
5159
_XEB_PHASED_FSIM_HANDLER_NAME = 'xeb_phased_fsim_characterization'
5260
_DEFAULT_XEB_CYCLE_DEPTHS = (5, 25, 50, 100, 200, 300)
61+
# Copied from cirq-google/cirq_google/engine/calibration_to_noise_properties.py
62+
GATE_ZPHASE_CODE_PAIRS: Dict[Type['cirq.Gate'], str] = {
63+
SycamoreGate: 'syc',
64+
cirq.ISwapPowGate: 'sqrt_iswap',
65+
}
5366

5467
T = TypeVar('T')
5568

@@ -331,6 +344,38 @@ def _json_dict_(self) -> Dict[str, Any]:
331344
}
332345

333346

347+
def to_zphase_data(results: Iterable[PhasedFSimCalibrationResult]) -> util.ZPhaseDataType:
348+
"""Packages a collection of results into ZPhaseDataType.
349+
350+
Args:
351+
results: List of results to pack into ZPhaseDataType. If multiple results provide a value
352+
for a given (gate, angle, qubits) tuple, only the last one will be kept.
353+
354+
Returns:
355+
A ZPhaseDataType-formatted result representation. This can be used with the
356+
calibration-to-noise pipeline for generating noise models.
357+
358+
Raises:
359+
ValueError: if results for a gate other than Sycamore or ISwapPowGate are given.
360+
"""
361+
zphase_data: util.ZPhaseDataType = {}
362+
for result in results:
363+
gate_type = GATE_ZPHASE_CODE_PAIRS.get(type(result.gate))
364+
if gate_type is None:
365+
raise ValueError(
366+
f"Only 'SycamoreGate' and 'ISwapPowGate' are supported, got {result.gate}"
367+
)
368+
gate_dict = zphase_data.setdefault(gate_type, {})
369+
for qubits, data in result.parameters.items():
370+
for angle, value in data.asdict().items():
371+
if value is None:
372+
continue
373+
angle_dict = gate_dict.setdefault(angle, {})
374+
angle_dict[qubits] = value
375+
376+
return zphase_data
377+
378+
334379
def merge_matching_results(
335380
results: Iterable[PhasedFSimCalibrationResult],
336381
) -> Optional[PhasedFSimCalibrationResult]:

cirq-google/cirq_google/calibration/phased_fsim_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
PhasedFSimCalibrationResult,
3737
WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
3838
merge_matching_results,
39+
to_zphase_data,
3940
try_convert_gate_to_fsim,
4041
try_convert_syc_or_sqrt_iswap_to_fsim,
4142
try_convert_sqrt_iswap_to_fsim,
@@ -625,6 +626,64 @@ def test_get_parameters():
625626
assert result.get_parameters(q_00, q_03) is None
626627

627628

629+
def test_to_zphase_data():
630+
q0, q1, q2 = cirq.GridQubit.rect(1, 3)
631+
result_1 = PhasedFSimCalibrationResult(
632+
{
633+
(q0, q1): PhasedFSimCharacterization(zeta=0.1, gamma=0.2),
634+
(q1, q2): PhasedFSimCharacterization(zeta=0.3, gamma=0.4),
635+
},
636+
gate=cirq_google.SycamoreGate(),
637+
options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
638+
)
639+
result_2 = PhasedFSimCalibrationResult(
640+
{
641+
(q0, q1): PhasedFSimCharacterization(zeta=0.5, gamma=0.6),
642+
(q1, q2): PhasedFSimCharacterization(zeta=0.7, gamma=0.8),
643+
},
644+
gate=cirq.ISwapPowGate(),
645+
options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
646+
)
647+
assert to_zphase_data([result_1, result_2]) == {
648+
'syc': {'zeta': {(q0, q1): 0.1, (q1, q2): 0.3}, 'gamma': {(q0, q1): 0.2, (q1, q2): 0.4}},
649+
'sqrt_iswap': {
650+
'zeta': {(q0, q1): 0.5, (q1, q2): 0.7},
651+
'gamma': {(q0, q1): 0.6, (q1, q2): 0.8},
652+
},
653+
}
654+
# Test update and override
655+
result_3 = PhasedFSimCalibrationResult(
656+
{
657+
(q0, q1): PhasedFSimCharacterization(theta=0.01),
658+
(q1, q2): PhasedFSimCharacterization(zeta=0.02),
659+
(q2, q0): PhasedFSimCharacterization(zeta=0.03, gamma=0.04, theta=0.05),
660+
},
661+
gate=cirq_google.SycamoreGate(),
662+
options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
663+
)
664+
assert to_zphase_data([result_1, result_3]) == {
665+
'syc': {
666+
'zeta': {(q0, q1): 0.1, (q1, q2): 0.02, (q2, q0): 0.03},
667+
'gamma': {(q0, q1): 0.2, (q1, q2): 0.4, (q2, q0): 0.04},
668+
'theta': {(q0, q1): 0.01, (q2, q0): 0.05},
669+
}
670+
}
671+
672+
673+
def test_to_zphase_unknown_gate_raises_error():
674+
q0, q1, q2 = cirq.GridQubit.rect(1, 3)
675+
result_1 = PhasedFSimCalibrationResult(
676+
{
677+
(q0, q1): PhasedFSimCharacterization(zeta=0.1, gamma=0.2),
678+
(q1, q2): PhasedFSimCharacterization(zeta=0.3, gamma=0.4),
679+
},
680+
gate=cirq.CZPowGate(),
681+
options=WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
682+
)
683+
with pytest.raises(ValueError, match="Only 'SycamoreGate' and 'ISwapPowGate' are supported"):
684+
_ = to_zphase_data([result_1])
685+
686+
628687
def test_merge_matching_results():
629688
q_00, q_01, q_02, q_03 = [cirq.GridQubit(0, index) for index in range(4)]
630689
gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0)

0 commit comments

Comments
 (0)