Skip to content

Add JSON serialization support for NoiseModel classes #7396

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

Merged
merged 42 commits into from
Jul 22, 2025

Conversation

WingCode
Copy link
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 wcourtney, vtomole, verult and a team 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
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.

Copy link

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
Contributor Author

WingCode commented Jun 7, 2025

@pavoljuhas I have fixed the CI checks.

Copy link
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.

@mhucka
Copy link
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.

@WingCode WingCode requested a review from pavoljuhas June 16, 2025 21:08
@mhucka mhucka assigned mhucka and WingCode and unassigned mhucka Jun 17, 2025
Copy link
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.

I have not got through all files, but I think we can address the new comments before next iteration.

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
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!

@pavoljuhas pavoljuhas added the priority/before-1.6 Finish before the Cirq 1.6 release label Jul 22, 2025
@pavoljuhas pavoljuhas added this pull request to the merge queue Jul 22, 2025
Merged via the queue into quantumlib:main with commit 8e8fd80 Jul 22, 2025
35 checks passed
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