Skip to content

Add JSON serialization support for NoiseModel classes#7396

Merged
pavoljuhas merged 42 commits intoquantumlib:mainfrom
WingCode:add-ser-noise-models
Jul 22, 2025
Merged

Add JSON serialization support for NoiseModel classes#7396
pavoljuhas merged 42 commits intoquantumlib:mainfrom
WingCode:add-ser-noise-models

Conversation

@WingCode
Copy link
Copy Markdown
Contributor

@WingCode WingCode commented May 30, 2025

This adds JSON serialization support and in most cases genuine repr support for

  • cirq.contrib.noise_models.DampedReadoutNoiseModel
  • cirq.contrib.noise_models.DepolarizingNoiseModel
  • cirq.contrib.noise_models.DepolarizingWithDampedReadoutNoiseModel
  • cirq.contrib.noise_models.DepolarizingWithReadoutNoiseModel
  • cirq.contrib.noise_models.ReadoutNoiseModel
  • cirq.devices.noise_properties.NoiseModelFromNoiseProperties
  • cirq.devices.thermal_noise_model.ThermalNoiseModel
  • cirq_google.experimental.noise_models.noise_models.PerQubitDepolarizingWithDampedReadoutNoiseModel

Also adds cirq.contrib.json_test_data for test data files specific to the classes in cirq.contrib module.

Fixes #3846

@WingCode WingCode requested review from a team, verult, vtomole and wcourtney as code owners May 30, 2025 17:34
@github-actions github-actions bot added the size: M 50< lines changed <250 label May 30, 2025
@github-actions github-actions bot added size: L 250< lines changed <1000 and removed size: M 50< lines changed <250 labels Jun 1, 2025
@pavoljuhas
Copy link
Copy Markdown
Collaborator

@WingCode - thank you for working on this. The failing CI checks should give some pointers on what is amiss. Feel free to ask here if you need clarification.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (c434f94) to head (ca36452).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7396    +/-   ##
========================================
  Coverage   98.68%   98.68%            
========================================
  Files        1091     1093     +2     
  Lines       96948    97109   +161     
========================================
+ Hits        95674    95835   +161     
  Misses       1274     1274            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WingCode
Copy link
Copy Markdown
Contributor Author

WingCode commented Jun 7, 2025

@pavoljuhas I have fixed the CI checks.

Comment thread cirq-core/cirq/contrib/noise_models/noise_models.py Outdated
Comment thread cirq-core/cirq/contrib/noise_models/noise_models.py Outdated
Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for serialization of contrib classes should be moved under cirq.contrib to limit any dependency of non-contrib cirq on cirq.contrib. Please see inline comments for other suggestions.

Comment thread cirq-core/cirq/json_resolver_cache.py Outdated
Comment thread cirq-core/cirq/protocols/json_test_data/DampedReadoutNoiseModel.json Outdated
Comment thread cirq-core/cirq/protocols/json_test_data/spec.py Outdated
Comment thread cirq-google/cirq_google/experimental/noise_models/noise_models.py Outdated
Comment thread cirq-google/cirq_google/experimental/noise_models/noise_models.py Outdated
Comment thread cirq-google/cirq_google/experimental/noise_models/noise_models.py Outdated
Comment thread cirq-google/cirq_google/experimental/noise_models/noise_models_test.py Outdated
Comment thread cirq-core/cirq/contrib/__init__.py Outdated
Comment thread cirq-core/cirq/devices/thermal_noise_model.py Outdated
Comment thread cirq-core/cirq/devices/thermal_noise_model_test.py Outdated
@mhucka
Copy link
Copy Markdown
Contributor

mhucka commented Jun 11, 2025

FYI that @pavoljuhas is out on jury duty (a mandatory civic obligation in the USA) starting today and possibly more days (depending on unpredictable factors). If he had commented on the PR or this issue before and hasn't responded, that's why.

Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
No need to change this interface to support JSON serialization.
Sync resolver cache with the contrib package `_class_resolver_dictionary`
and Move previously serializable classes from `should_not_be_serialized`
to `tested_elsewhere`.

Also add copyright notice.
- do not change public attributes convert them to protected
- use proper_repr for gate_durations_ns in repr
- exclude optional arguments from repr when unspecified
- change json schema to store class constructor arguments instead
  of object attributes - this allows to create instances in a
  standard way instead of using `cls.__new__`.
- in json store heat, cool, dephase rates as values only to be
  zipped with qubits in the same order.
Already tested by the presence of .json and .repr files.
Also check if optional rate arguments are skipped when unspecified.
- make the new noise-model class attributes read-only
- remove unnecessary `_from_json_dict_` methods
- test more json serde round trips over a list of instances
Drop unnecessary test of JSON serde and test more repr variants.
Keep sorted order as in main.  There was no change otherwise.
- store unspecified probabilities as None in JSON
- test serde and repr consistency of more instances in a list
- remove unnecessary methods `repr` and `_from_json_dict_`
- test serde round-trip in noise_properties_test with a custom resolver
- add NoiseModelFromNoiseProperties to cirq.json_resolver_cache
* Rewrite JSON support of NoiseModelFromNoiseProperties
* Review PerQubitDepolarizingWithDampedReadoutNoiseModel
* Revert cirq.protocols.json_test_data.spec
* Keep sorted order in cirq.json_resolver_cache
* Review noise_models_test
* Review cirq.contrib.noise_models
* Drop redundant test of ThermalNoiseModel serialization
* Revisit ThermalNoiseModel serialization
* Prefer `cache` to `lru_cache` for functions without arguments
* Clean up cirq.contrib.json_test_data.spec
* Restore cirq.contrib contents as it was before
@pavoljuhas pavoljuhas changed the title Add JSON serialisations Add JSON serialization support for NoiseModel classes Jul 21, 2025
Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about long delay in reviewing. I have pushed in
several changes that addressed the remaining comments.

Thank you for contributing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/before-1.6 Finish before the Cirq 1.6 release size: L 250< lines changed <1000 unitaryHACK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NoiseModels are not serializable

4 participants