diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000000..3d5e08e47d1d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Mark generated protobuf files to be collapsed by default in PRs +# See: https://github.com/github/linguist/#generated-code +*_pb2.py linguist-generated=true +*_pb2.pyi linguist-generated=true + diff --git a/cirq/__init__.py b/cirq/__init__.py index 6e0275a11689..b67357c84d03 100644 --- a/cirq/__init__.py +++ b/cirq/__init__.py @@ -117,6 +117,7 @@ bidiagonalize_unitary_with_special_orthogonals, block_diag, CONTROL_TAG, + deconstruct_single_qubit_matrix_into_angles, diagonalize_real_symmetric_and_sorted_diagonal_matrices, diagonalize_real_symmetric_matrix, dot, @@ -139,6 +140,7 @@ kron_with_controls, map_eigenvalues, match_global_phase, + matrix_commutes, matrix_from_basis_coefficients, partial_trace, partial_trace_of_state_vector_as_mixture, @@ -315,6 +317,7 @@ STATE_VECTOR_LIKE, to_valid_density_matrix, to_valid_state_vector, + validate_indices, validate_normalized_state, validate_normalized_state_vector, validate_qid_shape, @@ -379,6 +382,7 @@ Sweepable, to_resolvers, to_sweep, + to_sweeps, TrialResult, UnitSweep, Zip, @@ -479,6 +483,7 @@ to_json, obj_to_dict_helper, trace_distance_bound, + trace_distance_from_angle_list, unitary, validate_mixture, ) diff --git a/cirq/google/api/v1/operations_pb2.py b/cirq/google/api/v1/operations_pb2.py index 34c8d2f22e87..d178d03ebcc1 100644 --- a/cirq/google/api/v1/operations_pb2.py +++ b/cirq/google/api/v1/operations_pb2.py @@ -379,53 +379,53 @@ DESCRIPTOR.message_types_by_name['Operation'] = _OPERATION _sym_db.RegisterFileDescriptor(DESCRIPTOR) -Qubit = _reflection.GeneratedProtocolMessageType('Qubit', (_message.Message,), dict( - DESCRIPTOR = _QUBIT, - __module__ = 'cirq.google.api.v1.operations_pb2' +Qubit = _reflection.GeneratedProtocolMessageType('Qubit', (_message.Message,), { + 'DESCRIPTOR' : _QUBIT, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Qubit) - )) + }) _sym_db.RegisterMessage(Qubit) -ParameterizedFloat = _reflection.GeneratedProtocolMessageType('ParameterizedFloat', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERIZEDFLOAT, - __module__ = 'cirq.google.api.v1.operations_pb2' +ParameterizedFloat = _reflection.GeneratedProtocolMessageType('ParameterizedFloat', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERIZEDFLOAT, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ParameterizedFloat) - )) + }) _sym_db.RegisterMessage(ParameterizedFloat) -ExpW = _reflection.GeneratedProtocolMessageType('ExpW', (_message.Message,), dict( - DESCRIPTOR = _EXPW, - __module__ = 'cirq.google.api.v1.operations_pb2' +ExpW = _reflection.GeneratedProtocolMessageType('ExpW', (_message.Message,), { + 'DESCRIPTOR' : _EXPW, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ExpW) - )) + }) _sym_db.RegisterMessage(ExpW) -ExpZ = _reflection.GeneratedProtocolMessageType('ExpZ', (_message.Message,), dict( - DESCRIPTOR = _EXPZ, - __module__ = 'cirq.google.api.v1.operations_pb2' +ExpZ = _reflection.GeneratedProtocolMessageType('ExpZ', (_message.Message,), { + 'DESCRIPTOR' : _EXPZ, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ExpZ) - )) + }) _sym_db.RegisterMessage(ExpZ) -Exp11 = _reflection.GeneratedProtocolMessageType('Exp11', (_message.Message,), dict( - DESCRIPTOR = _EXP11, - __module__ = 'cirq.google.api.v1.operations_pb2' +Exp11 = _reflection.GeneratedProtocolMessageType('Exp11', (_message.Message,), { + 'DESCRIPTOR' : _EXP11, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Exp11) - )) + }) _sym_db.RegisterMessage(Exp11) -Measurement = _reflection.GeneratedProtocolMessageType('Measurement', (_message.Message,), dict( - DESCRIPTOR = _MEASUREMENT, - __module__ = 'cirq.google.api.v1.operations_pb2' +Measurement = _reflection.GeneratedProtocolMessageType('Measurement', (_message.Message,), { + 'DESCRIPTOR' : _MEASUREMENT, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Measurement) - )) + }) _sym_db.RegisterMessage(Measurement) -Operation = _reflection.GeneratedProtocolMessageType('Operation', (_message.Message,), dict( - DESCRIPTOR = _OPERATION, - __module__ = 'cirq.google.api.v1.operations_pb2' +Operation = _reflection.GeneratedProtocolMessageType('Operation', (_message.Message,), { + 'DESCRIPTOR' : _OPERATION, + '__module__' : 'cirq.google.api.v1.operations_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Operation) - )) + }) _sym_db.RegisterMessage(Operation) diff --git a/cirq/google/api/v1/params_pb2.py b/cirq/google/api/v1/params_pb2.py index c97b5e0a0eb7..53fe31970461 100644 --- a/cirq/google/api/v1/params_pb2.py +++ b/cirq/google/api/v1/params_pb2.py @@ -339,60 +339,60 @@ DESCRIPTOR.message_types_by_name['ParameterDict'] = _PARAMETERDICT _sym_db.RegisterFileDescriptor(DESCRIPTOR) -ParameterSweep = _reflection.GeneratedProtocolMessageType('ParameterSweep', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERSWEEP, - __module__ = 'cirq.google.api.v1.params_pb2' +ParameterSweep = _reflection.GeneratedProtocolMessageType('ParameterSweep', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERSWEEP, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ParameterSweep) - )) + }) _sym_db.RegisterMessage(ParameterSweep) -ProductSweep = _reflection.GeneratedProtocolMessageType('ProductSweep', (_message.Message,), dict( - DESCRIPTOR = _PRODUCTSWEEP, - __module__ = 'cirq.google.api.v1.params_pb2' +ProductSweep = _reflection.GeneratedProtocolMessageType('ProductSweep', (_message.Message,), { + 'DESCRIPTOR' : _PRODUCTSWEEP, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ProductSweep) - )) + }) _sym_db.RegisterMessage(ProductSweep) -ZipSweep = _reflection.GeneratedProtocolMessageType('ZipSweep', (_message.Message,), dict( - DESCRIPTOR = _ZIPSWEEP, - __module__ = 'cirq.google.api.v1.params_pb2' +ZipSweep = _reflection.GeneratedProtocolMessageType('ZipSweep', (_message.Message,), { + 'DESCRIPTOR' : _ZIPSWEEP, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ZipSweep) - )) + }) _sym_db.RegisterMessage(ZipSweep) -SingleSweep = _reflection.GeneratedProtocolMessageType('SingleSweep', (_message.Message,), dict( - DESCRIPTOR = _SINGLESWEEP, - __module__ = 'cirq.google.api.v1.params_pb2' +SingleSweep = _reflection.GeneratedProtocolMessageType('SingleSweep', (_message.Message,), { + 'DESCRIPTOR' : _SINGLESWEEP, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.SingleSweep) - )) + }) _sym_db.RegisterMessage(SingleSweep) -Points = _reflection.GeneratedProtocolMessageType('Points', (_message.Message,), dict( - DESCRIPTOR = _POINTS, - __module__ = 'cirq.google.api.v1.params_pb2' +Points = _reflection.GeneratedProtocolMessageType('Points', (_message.Message,), { + 'DESCRIPTOR' : _POINTS, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Points) - )) + }) _sym_db.RegisterMessage(Points) -Linspace = _reflection.GeneratedProtocolMessageType('Linspace', (_message.Message,), dict( - DESCRIPTOR = _LINSPACE, - __module__ = 'cirq.google.api.v1.params_pb2' +Linspace = _reflection.GeneratedProtocolMessageType('Linspace', (_message.Message,), { + 'DESCRIPTOR' : _LINSPACE, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Linspace) - )) + }) _sym_db.RegisterMessage(Linspace) -ParameterDict = _reflection.GeneratedProtocolMessageType('ParameterDict', (_message.Message,), dict( +ParameterDict = _reflection.GeneratedProtocolMessageType('ParameterDict', (_message.Message,), { - AssignmentsEntry = _reflection.GeneratedProtocolMessageType('AssignmentsEntry', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERDICT_ASSIGNMENTSENTRY, - __module__ = 'cirq.google.api.v1.params_pb2' + 'AssignmentsEntry' : _reflection.GeneratedProtocolMessageType('AssignmentsEntry', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERDICT_ASSIGNMENTSENTRY, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ParameterDict.AssignmentsEntry) - )) + }) , - DESCRIPTOR = _PARAMETERDICT, - __module__ = 'cirq.google.api.v1.params_pb2' + 'DESCRIPTOR' : _PARAMETERDICT, + '__module__' : 'cirq.google.api.v1.params_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ParameterDict) - )) + }) _sym_db.RegisterMessage(ParameterDict) _sym_db.RegisterMessage(ParameterDict.AssignmentsEntry) diff --git a/cirq/google/api/v1/program_pb2.py b/cirq/google/api/v1/program_pb2.py index e20e967b08e0..c59833be3a07 100644 --- a/cirq/google/api/v1/program_pb2.py +++ b/cirq/google/api/v1/program_pb2.py @@ -265,46 +265,46 @@ DESCRIPTOR.message_types_by_name['Result'] = _RESULT _sym_db.RegisterFileDescriptor(DESCRIPTOR) -Program = _reflection.GeneratedProtocolMessageType('Program', (_message.Message,), dict( - DESCRIPTOR = _PROGRAM, - __module__ = 'cirq.google.api.v1.program_pb2' +Program = _reflection.GeneratedProtocolMessageType('Program', (_message.Message,), { + 'DESCRIPTOR' : _PROGRAM, + '__module__' : 'cirq.google.api.v1.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Program) - )) + }) _sym_db.RegisterMessage(Program) -RunContext = _reflection.GeneratedProtocolMessageType('RunContext', (_message.Message,), dict( - DESCRIPTOR = _RUNCONTEXT, - __module__ = 'cirq.google.api.v1.program_pb2' +RunContext = _reflection.GeneratedProtocolMessageType('RunContext', (_message.Message,), { + 'DESCRIPTOR' : _RUNCONTEXT, + '__module__' : 'cirq.google.api.v1.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.RunContext) - )) + }) _sym_db.RegisterMessage(RunContext) -ParameterizedResult = _reflection.GeneratedProtocolMessageType('ParameterizedResult', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERIZEDRESULT, - __module__ = 'cirq.google.api.v1.program_pb2' +ParameterizedResult = _reflection.GeneratedProtocolMessageType('ParameterizedResult', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERIZEDRESULT, + '__module__' : 'cirq.google.api.v1.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.ParameterizedResult) - )) + }) _sym_db.RegisterMessage(ParameterizedResult) -MeasurementKey = _reflection.GeneratedProtocolMessageType('MeasurementKey', (_message.Message,), dict( - DESCRIPTOR = _MEASUREMENTKEY, - __module__ = 'cirq.google.api.v1.program_pb2' +MeasurementKey = _reflection.GeneratedProtocolMessageType('MeasurementKey', (_message.Message,), { + 'DESCRIPTOR' : _MEASUREMENTKEY, + '__module__' : 'cirq.google.api.v1.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.MeasurementKey) - )) + }) _sym_db.RegisterMessage(MeasurementKey) -SweepResult = _reflection.GeneratedProtocolMessageType('SweepResult', (_message.Message,), dict( - DESCRIPTOR = _SWEEPRESULT, - __module__ = 'cirq.google.api.v1.program_pb2' +SweepResult = _reflection.GeneratedProtocolMessageType('SweepResult', (_message.Message,), { + 'DESCRIPTOR' : _SWEEPRESULT, + '__module__' : 'cirq.google.api.v1.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.SweepResult) - )) + }) _sym_db.RegisterMessage(SweepResult) -Result = _reflection.GeneratedProtocolMessageType('Result', (_message.Message,), dict( - DESCRIPTOR = _RESULT, - __module__ = 'cirq.google.api.v1.program_pb2' +Result = _reflection.GeneratedProtocolMessageType('Result', (_message.Message,), { + 'DESCRIPTOR' : _RESULT, + '__module__' : 'cirq.google.api.v1.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v1.Result) - )) + }) _sym_db.RegisterMessage(Result) diff --git a/cirq/google/api/v2/BUILD b/cirq/google/api/v2/BUILD index eed752f5fd9e..253249524880 100644 --- a/cirq/google/api/v2/BUILD +++ b/cirq/google/api/v2/BUILD @@ -2,6 +2,33 @@ package(default_visibility = ["//visibility:public"]) load("@com_google_protobuf//:protobuf.bzl", "py_proto_library") +proto_library( + name = "batch_proto", + srcs = ["batch.proto"], + deps = [ + ":program_proto", + ":result_proto", + ":run_context_proto" + ], +) + +py_proto_library( + name = "batch_py_proto", + srcs = ["batch.proto"], + deps = [ + ":program_py_proto", + ":result_py_proto", + ":run_context_py_proto" + ], +) + +cc_proto_library( + name = "batch_cc_proto", + deps = [ + ":batch_proto" + ], +) + proto_library( name = "device_proto", srcs = ["device.proto"], diff --git a/cirq/google/api/v2/batch.proto b/cirq/google/api/v2/batch.proto new file mode 100644 index 000000000000..d338a5238dc9 --- /dev/null +++ b/cirq/google/api/v2/batch.proto @@ -0,0 +1,58 @@ +syntax = "proto3"; + +import "cirq/google/api/v2/program.proto"; +import "cirq/google/api/v2/result.proto"; +import "cirq/google/api/v2/run_context.proto"; + +package cirq.google.api.v2; + +option java_package = "com.google.cirq.google.api.v2"; +option java_outer_classname = "BatchProto"; +option java_multiple_files = true; + +// A Batch of multiple circuits that should be run together +// as one QuantumProgram within Quantum Engine. +// +// Note: Batching is done on a best-effort basis. +// Circuits will be be bundled together, but the size +// of the total batch and different hardware constraints may +// cause the programs to be executed seperately on the hardware. +message BatchProgram { + + // The circuits that should be bundled together as one program + repeated Program programs = 1; +} + +// A batch of contexts for running a bundled batch of programs +// To be used in conjunction with BatchProgram +message BatchRunContext { + + // Run contexts for each program in the BatchProgram + // Each RunContext should map directly to a Program in the corresponding + // BatchProgram. + // + // This message must have one RunContext for each Program in the + // BatchProgram, and the order of the RunContext messages should + // match the order of the Programs in the BatchProgram. + repeated RunContext run_contexts = 1; +} + + +// The result returned from running a BatchProgram/BatchRunContext +message BatchResult { + + // Results returned from executing a BatchProgram/BatchRunContext pair. + // + // After a BatchProgram and BatchRunContext is successfully run in + // Quantum Engine, the expected result if successful will be a BatchResult. + // + // Each Result in this message will directly correspond to a Program/ + // RunContext pair in the request. There will be one Result in this message + // for each corresponding pair in the request and the order of the Results + // will match the order of the Programs from the request. + // + // In case of partial results, an empty (default) Result object will be + // populated for programs that were not able to be run correctly. + repeated Result results = 1; +} + diff --git a/cirq/google/api/v2/device_pb2.py b/cirq/google/api/v2/device_pb2.py index 828fa4b31ea9..c286442dc2da 100644 --- a/cirq/google/api/v2/device_pb2.py +++ b/cirq/google/api/v2/device_pb2.py @@ -414,53 +414,53 @@ DESCRIPTOR.message_types_by_name['Target'] = _TARGET _sym_db.RegisterFileDescriptor(DESCRIPTOR) -DeviceSpecification = _reflection.GeneratedProtocolMessageType('DeviceSpecification', (_message.Message,), dict( - DESCRIPTOR = _DEVICESPECIFICATION, - __module__ = 'cirq.google.api.v2.device_pb2' +DeviceSpecification = _reflection.GeneratedProtocolMessageType('DeviceSpecification', (_message.Message,), { + 'DESCRIPTOR' : _DEVICESPECIFICATION, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.DeviceSpecification) - )) + }) _sym_db.RegisterMessage(DeviceSpecification) -GateSet = _reflection.GeneratedProtocolMessageType('GateSet', (_message.Message,), dict( - DESCRIPTOR = _GATESET, - __module__ = 'cirq.google.api.v2.device_pb2' +GateSet = _reflection.GeneratedProtocolMessageType('GateSet', (_message.Message,), { + 'DESCRIPTOR' : _GATESET, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.GateSet) - )) + }) _sym_db.RegisterMessage(GateSet) -GateDefinition = _reflection.GeneratedProtocolMessageType('GateDefinition', (_message.Message,), dict( - DESCRIPTOR = _GATEDEFINITION, - __module__ = 'cirq.google.api.v2.device_pb2' +GateDefinition = _reflection.GeneratedProtocolMessageType('GateDefinition', (_message.Message,), { + 'DESCRIPTOR' : _GATEDEFINITION, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.GateDefinition) - )) + }) _sym_db.RegisterMessage(GateDefinition) -ArgDefinition = _reflection.GeneratedProtocolMessageType('ArgDefinition', (_message.Message,), dict( - DESCRIPTOR = _ARGDEFINITION, - __module__ = 'cirq.google.api.v2.device_pb2' +ArgDefinition = _reflection.GeneratedProtocolMessageType('ArgDefinition', (_message.Message,), { + 'DESCRIPTOR' : _ARGDEFINITION, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ArgDefinition) - )) + }) _sym_db.RegisterMessage(ArgDefinition) -ArgumentRange = _reflection.GeneratedProtocolMessageType('ArgumentRange', (_message.Message,), dict( - DESCRIPTOR = _ARGUMENTRANGE, - __module__ = 'cirq.google.api.v2.device_pb2' +ArgumentRange = _reflection.GeneratedProtocolMessageType('ArgumentRange', (_message.Message,), { + 'DESCRIPTOR' : _ARGUMENTRANGE, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ArgumentRange) - )) + }) _sym_db.RegisterMessage(ArgumentRange) -TargetSet = _reflection.GeneratedProtocolMessageType('TargetSet', (_message.Message,), dict( - DESCRIPTOR = _TARGETSET, - __module__ = 'cirq.google.api.v2.device_pb2' +TargetSet = _reflection.GeneratedProtocolMessageType('TargetSet', (_message.Message,), { + 'DESCRIPTOR' : _TARGETSET, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.TargetSet) - )) + }) _sym_db.RegisterMessage(TargetSet) -Target = _reflection.GeneratedProtocolMessageType('Target', (_message.Message,), dict( - DESCRIPTOR = _TARGET, - __module__ = 'cirq.google.api.v2.device_pb2' +Target = _reflection.GeneratedProtocolMessageType('Target', (_message.Message,), { + 'DESCRIPTOR' : _TARGET, + '__module__' : 'cirq.google.api.v2.device_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Target) - )) + }) _sym_db.RegisterMessage(Target) diff --git a/cirq/google/api/v2/metrics_pb2.py b/cirq/google/api/v2/metrics_pb2.py index 37d6e05c90f6..65936a5c1c0f 100644 --- a/cirq/google/api/v2/metrics_pb2.py +++ b/cirq/google/api/v2/metrics_pb2.py @@ -182,25 +182,25 @@ DESCRIPTOR.message_types_by_name['Value'] = _VALUE _sym_db.RegisterFileDescriptor(DESCRIPTOR) -MetricsSnapshot = _reflection.GeneratedProtocolMessageType('MetricsSnapshot', (_message.Message,), dict( - DESCRIPTOR = _METRICSSNAPSHOT, - __module__ = 'cirq.google.api.v2.metrics_pb2' +MetricsSnapshot = _reflection.GeneratedProtocolMessageType('MetricsSnapshot', (_message.Message,), { + 'DESCRIPTOR' : _METRICSSNAPSHOT, + '__module__' : 'cirq.google.api.v2.metrics_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.MetricsSnapshot) - )) + }) _sym_db.RegisterMessage(MetricsSnapshot) -Metric = _reflection.GeneratedProtocolMessageType('Metric', (_message.Message,), dict( - DESCRIPTOR = _METRIC, - __module__ = 'cirq.google.api.v2.metrics_pb2' +Metric = _reflection.GeneratedProtocolMessageType('Metric', (_message.Message,), { + 'DESCRIPTOR' : _METRIC, + '__module__' : 'cirq.google.api.v2.metrics_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Metric) - )) + }) _sym_db.RegisterMessage(Metric) -Value = _reflection.GeneratedProtocolMessageType('Value', (_message.Message,), dict( - DESCRIPTOR = _VALUE, - __module__ = 'cirq.google.api.v2.metrics_pb2' +Value = _reflection.GeneratedProtocolMessageType('Value', (_message.Message,), { + 'DESCRIPTOR' : _VALUE, + '__module__' : 'cirq.google.api.v2.metrics_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Value) - )) + }) _sym_db.RegisterMessage(Value) diff --git a/cirq/google/api/v2/program_pb2.py b/cirq/google/api/v2/program_pb2.py index c7784c3fac73..1cb4dbb0f459 100644 --- a/cirq/google/api/v2/program_pb2.py +++ b/cirq/google/api/v2/program_pb2.py @@ -638,103 +638,103 @@ DESCRIPTOR.message_types_by_name['ArgFunction'] = _ARGFUNCTION _sym_db.RegisterFileDescriptor(DESCRIPTOR) -Program = _reflection.GeneratedProtocolMessageType('Program', (_message.Message,), dict( - DESCRIPTOR = _PROGRAM, - __module__ = 'cirq.google.api.v2.program_pb2' +Program = _reflection.GeneratedProtocolMessageType('Program', (_message.Message,), { + 'DESCRIPTOR' : _PROGRAM, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Program) - )) + }) _sym_db.RegisterMessage(Program) -Circuit = _reflection.GeneratedProtocolMessageType('Circuit', (_message.Message,), dict( - DESCRIPTOR = _CIRCUIT, - __module__ = 'cirq.google.api.v2.program_pb2' +Circuit = _reflection.GeneratedProtocolMessageType('Circuit', (_message.Message,), { + 'DESCRIPTOR' : _CIRCUIT, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Circuit) - )) + }) _sym_db.RegisterMessage(Circuit) -Moment = _reflection.GeneratedProtocolMessageType('Moment', (_message.Message,), dict( - DESCRIPTOR = _MOMENT, - __module__ = 'cirq.google.api.v2.program_pb2' +Moment = _reflection.GeneratedProtocolMessageType('Moment', (_message.Message,), { + 'DESCRIPTOR' : _MOMENT, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Moment) - )) + }) _sym_db.RegisterMessage(Moment) -Schedule = _reflection.GeneratedProtocolMessageType('Schedule', (_message.Message,), dict( - DESCRIPTOR = _SCHEDULE, - __module__ = 'cirq.google.api.v2.program_pb2' +Schedule = _reflection.GeneratedProtocolMessageType('Schedule', (_message.Message,), { + 'DESCRIPTOR' : _SCHEDULE, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Schedule) - )) + }) _sym_db.RegisterMessage(Schedule) -ScheduledOperation = _reflection.GeneratedProtocolMessageType('ScheduledOperation', (_message.Message,), dict( - DESCRIPTOR = _SCHEDULEDOPERATION, - __module__ = 'cirq.google.api.v2.program_pb2' +ScheduledOperation = _reflection.GeneratedProtocolMessageType('ScheduledOperation', (_message.Message,), { + 'DESCRIPTOR' : _SCHEDULEDOPERATION, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ScheduledOperation) - )) + }) _sym_db.RegisterMessage(ScheduledOperation) -Language = _reflection.GeneratedProtocolMessageType('Language', (_message.Message,), dict( - DESCRIPTOR = _LANGUAGE, - __module__ = 'cirq.google.api.v2.program_pb2' +Language = _reflection.GeneratedProtocolMessageType('Language', (_message.Message,), { + 'DESCRIPTOR' : _LANGUAGE, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Language) - )) + }) _sym_db.RegisterMessage(Language) -Operation = _reflection.GeneratedProtocolMessageType('Operation', (_message.Message,), dict( +Operation = _reflection.GeneratedProtocolMessageType('Operation', (_message.Message,), { - ArgsEntry = _reflection.GeneratedProtocolMessageType('ArgsEntry', (_message.Message,), dict( - DESCRIPTOR = _OPERATION_ARGSENTRY, - __module__ = 'cirq.google.api.v2.program_pb2' + 'ArgsEntry' : _reflection.GeneratedProtocolMessageType('ArgsEntry', (_message.Message,), { + 'DESCRIPTOR' : _OPERATION_ARGSENTRY, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Operation.ArgsEntry) - )) + }) , - DESCRIPTOR = _OPERATION, - __module__ = 'cirq.google.api.v2.program_pb2' + 'DESCRIPTOR' : _OPERATION, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Operation) - )) + }) _sym_db.RegisterMessage(Operation) _sym_db.RegisterMessage(Operation.ArgsEntry) -Gate = _reflection.GeneratedProtocolMessageType('Gate', (_message.Message,), dict( - DESCRIPTOR = _GATE, - __module__ = 'cirq.google.api.v2.program_pb2' +Gate = _reflection.GeneratedProtocolMessageType('Gate', (_message.Message,), { + 'DESCRIPTOR' : _GATE, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Gate) - )) + }) _sym_db.RegisterMessage(Gate) -Qubit = _reflection.GeneratedProtocolMessageType('Qubit', (_message.Message,), dict( - DESCRIPTOR = _QUBIT, - __module__ = 'cirq.google.api.v2.program_pb2' +Qubit = _reflection.GeneratedProtocolMessageType('Qubit', (_message.Message,), { + 'DESCRIPTOR' : _QUBIT, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Qubit) - )) + }) _sym_db.RegisterMessage(Qubit) -Arg = _reflection.GeneratedProtocolMessageType('Arg', (_message.Message,), dict( - DESCRIPTOR = _ARG, - __module__ = 'cirq.google.api.v2.program_pb2' +Arg = _reflection.GeneratedProtocolMessageType('Arg', (_message.Message,), { + 'DESCRIPTOR' : _ARG, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Arg) - )) + }) _sym_db.RegisterMessage(Arg) -ArgValue = _reflection.GeneratedProtocolMessageType('ArgValue', (_message.Message,), dict( - DESCRIPTOR = _ARGVALUE, - __module__ = 'cirq.google.api.v2.program_pb2' +ArgValue = _reflection.GeneratedProtocolMessageType('ArgValue', (_message.Message,), { + 'DESCRIPTOR' : _ARGVALUE, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ArgValue) - )) + }) _sym_db.RegisterMessage(ArgValue) -RepeatedBoolean = _reflection.GeneratedProtocolMessageType('RepeatedBoolean', (_message.Message,), dict( - DESCRIPTOR = _REPEATEDBOOLEAN, - __module__ = 'cirq.google.api.v2.program_pb2' +RepeatedBoolean = _reflection.GeneratedProtocolMessageType('RepeatedBoolean', (_message.Message,), { + 'DESCRIPTOR' : _REPEATEDBOOLEAN, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.RepeatedBoolean) - )) + }) _sym_db.RegisterMessage(RepeatedBoolean) -ArgFunction = _reflection.GeneratedProtocolMessageType('ArgFunction', (_message.Message,), dict( - DESCRIPTOR = _ARGFUNCTION, - __module__ = 'cirq.google.api.v2.program_pb2' +ArgFunction = _reflection.GeneratedProtocolMessageType('ArgFunction', (_message.Message,), { + 'DESCRIPTOR' : _ARGFUNCTION, + '__module__' : 'cirq.google.api.v2.program_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ArgFunction) - )) + }) _sym_db.RegisterMessage(ArgFunction) diff --git a/cirq/google/api/v2/result_pb2.py b/cirq/google/api/v2/result_pb2.py index 7471dfc058d3..79f7e34d51ae 100644 --- a/cirq/google/api/v2/result_pb2.py +++ b/cirq/google/api/v2/result_pb2.py @@ -294,53 +294,53 @@ DESCRIPTOR.message_types_by_name['ParameterDict'] = _PARAMETERDICT _sym_db.RegisterFileDescriptor(DESCRIPTOR) -Result = _reflection.GeneratedProtocolMessageType('Result', (_message.Message,), dict( - DESCRIPTOR = _RESULT, - __module__ = 'cirq.google.api.v2.result_pb2' +Result = _reflection.GeneratedProtocolMessageType('Result', (_message.Message,), { + 'DESCRIPTOR' : _RESULT, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Result) - )) + }) _sym_db.RegisterMessage(Result) -SweepResult = _reflection.GeneratedProtocolMessageType('SweepResult', (_message.Message,), dict( - DESCRIPTOR = _SWEEPRESULT, - __module__ = 'cirq.google.api.v2.result_pb2' +SweepResult = _reflection.GeneratedProtocolMessageType('SweepResult', (_message.Message,), { + 'DESCRIPTOR' : _SWEEPRESULT, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.SweepResult) - )) + }) _sym_db.RegisterMessage(SweepResult) -ParameterizedResult = _reflection.GeneratedProtocolMessageType('ParameterizedResult', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERIZEDRESULT, - __module__ = 'cirq.google.api.v2.result_pb2' +ParameterizedResult = _reflection.GeneratedProtocolMessageType('ParameterizedResult', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERIZEDRESULT, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ParameterizedResult) - )) + }) _sym_db.RegisterMessage(ParameterizedResult) -MeasurementResult = _reflection.GeneratedProtocolMessageType('MeasurementResult', (_message.Message,), dict( - DESCRIPTOR = _MEASUREMENTRESULT, - __module__ = 'cirq.google.api.v2.result_pb2' +MeasurementResult = _reflection.GeneratedProtocolMessageType('MeasurementResult', (_message.Message,), { + 'DESCRIPTOR' : _MEASUREMENTRESULT, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.MeasurementResult) - )) + }) _sym_db.RegisterMessage(MeasurementResult) -QubitMeasurementResult = _reflection.GeneratedProtocolMessageType('QubitMeasurementResult', (_message.Message,), dict( - DESCRIPTOR = _QUBITMEASUREMENTRESULT, - __module__ = 'cirq.google.api.v2.result_pb2' +QubitMeasurementResult = _reflection.GeneratedProtocolMessageType('QubitMeasurementResult', (_message.Message,), { + 'DESCRIPTOR' : _QUBITMEASUREMENTRESULT, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.QubitMeasurementResult) - )) + }) _sym_db.RegisterMessage(QubitMeasurementResult) -ParameterDict = _reflection.GeneratedProtocolMessageType('ParameterDict', (_message.Message,), dict( +ParameterDict = _reflection.GeneratedProtocolMessageType('ParameterDict', (_message.Message,), { - AssignmentsEntry = _reflection.GeneratedProtocolMessageType('AssignmentsEntry', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERDICT_ASSIGNMENTSENTRY, - __module__ = 'cirq.google.api.v2.result_pb2' + 'AssignmentsEntry' : _reflection.GeneratedProtocolMessageType('AssignmentsEntry', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERDICT_ASSIGNMENTSENTRY, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ParameterDict.AssignmentsEntry) - )) + }) , - DESCRIPTOR = _PARAMETERDICT, - __module__ = 'cirq.google.api.v2.result_pb2' + 'DESCRIPTOR' : _PARAMETERDICT, + '__module__' : 'cirq.google.api.v2.result_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ParameterDict) - )) + }) _sym_db.RegisterMessage(ParameterDict) _sym_db.RegisterMessage(ParameterDict.AssignmentsEntry) diff --git a/cirq/google/api/v2/run_context_pb2.py b/cirq/google/api/v2/run_context_pb2.py index e017add9d68a..dfb8dd7096c7 100644 --- a/cirq/google/api/v2/run_context_pb2.py +++ b/cirq/google/api/v2/run_context_pb2.py @@ -354,53 +354,53 @@ DESCRIPTOR.message_types_by_name['Linspace'] = _LINSPACE _sym_db.RegisterFileDescriptor(DESCRIPTOR) -RunContext = _reflection.GeneratedProtocolMessageType('RunContext', (_message.Message,), dict( - DESCRIPTOR = _RUNCONTEXT, - __module__ = 'cirq.google.api.v2.run_context_pb2' +RunContext = _reflection.GeneratedProtocolMessageType('RunContext', (_message.Message,), { + 'DESCRIPTOR' : _RUNCONTEXT, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.RunContext) - )) + }) _sym_db.RegisterMessage(RunContext) -ParameterSweep = _reflection.GeneratedProtocolMessageType('ParameterSweep', (_message.Message,), dict( - DESCRIPTOR = _PARAMETERSWEEP, - __module__ = 'cirq.google.api.v2.run_context_pb2' +ParameterSweep = _reflection.GeneratedProtocolMessageType('ParameterSweep', (_message.Message,), { + 'DESCRIPTOR' : _PARAMETERSWEEP, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.ParameterSweep) - )) + }) _sym_db.RegisterMessage(ParameterSweep) -Sweep = _reflection.GeneratedProtocolMessageType('Sweep', (_message.Message,), dict( - DESCRIPTOR = _SWEEP, - __module__ = 'cirq.google.api.v2.run_context_pb2' +Sweep = _reflection.GeneratedProtocolMessageType('Sweep', (_message.Message,), { + 'DESCRIPTOR' : _SWEEP, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Sweep) - )) + }) _sym_db.RegisterMessage(Sweep) -SweepFunction = _reflection.GeneratedProtocolMessageType('SweepFunction', (_message.Message,), dict( - DESCRIPTOR = _SWEEPFUNCTION, - __module__ = 'cirq.google.api.v2.run_context_pb2' +SweepFunction = _reflection.GeneratedProtocolMessageType('SweepFunction', (_message.Message,), { + 'DESCRIPTOR' : _SWEEPFUNCTION, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.SweepFunction) - )) + }) _sym_db.RegisterMessage(SweepFunction) -SingleSweep = _reflection.GeneratedProtocolMessageType('SingleSweep', (_message.Message,), dict( - DESCRIPTOR = _SINGLESWEEP, - __module__ = 'cirq.google.api.v2.run_context_pb2' +SingleSweep = _reflection.GeneratedProtocolMessageType('SingleSweep', (_message.Message,), { + 'DESCRIPTOR' : _SINGLESWEEP, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.SingleSweep) - )) + }) _sym_db.RegisterMessage(SingleSweep) -Points = _reflection.GeneratedProtocolMessageType('Points', (_message.Message,), dict( - DESCRIPTOR = _POINTS, - __module__ = 'cirq.google.api.v2.run_context_pb2' +Points = _reflection.GeneratedProtocolMessageType('Points', (_message.Message,), { + 'DESCRIPTOR' : _POINTS, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Points) - )) + }) _sym_db.RegisterMessage(Points) -Linspace = _reflection.GeneratedProtocolMessageType('Linspace', (_message.Message,), dict( - DESCRIPTOR = _LINSPACE, - __module__ = 'cirq.google.api.v2.run_context_pb2' +Linspace = _reflection.GeneratedProtocolMessageType('Linspace', (_message.Message,), { + 'DESCRIPTOR' : _LINSPACE, + '__module__' : 'cirq.google.api.v2.run_context_pb2' # @@protoc_insertion_point(class_scope:cirq.google.api.v2.Linspace) - )) + }) _sym_db.RegisterMessage(Linspace) diff --git a/cirq/google/engine/engine_job.py b/cirq/google/engine/engine_job.py index 78a006d7366d..a3cad072867a 100644 --- a/cirq/google/engine/engine_job.py +++ b/cirq/google/engine/engine_job.py @@ -213,8 +213,8 @@ def _deserialize_run_context(run_context: quantum.types.any_pb2.Any raise ValueError('deserializing a v1 RunContext is not supported') if (run_context_type == 'cirq.google.api.v2.RunContext' or run_context_type == 'cirq.api.google.v2.RunContext'): - v2_run_context = v2.run_context_pb2.RunContext() - v2_run_context.ParseFromString(run_context.value) + v2_run_context = v2.run_context_pb2.RunContext.FromString( + run_context.value) return v2_run_context.parameter_sweeps[0].repetitions, [ v2.sweep_from_proto(s.sweep) for s in v2_run_context.parameter_sweeps @@ -242,8 +242,7 @@ def get_calibration(self) -> Optional[calibration.Calibration]: ids = self.context.client._ids_from_calibration_name( status.calibration_name) response = self.context.client.get_calibration(*ids) - metrics = v2.metrics_pb2.MetricsSnapshot() - metrics.ParseFromString(response.data.value) + metrics = v2.metrics_pb2.MetricsSnapshot.FromString(response.data.value) return calibration.Calibration(metrics) def cancel(self) -> None: @@ -279,13 +278,12 @@ def results(self) -> List[study.TrialResult]: result_type = result.type_url[len(engine_base.TYPE_PREFIX):] if (result_type == 'cirq.google.api.v1.Result' or result_type == 'cirq.api.google.v1.Result'): - v1_parsed_result = v1.program_pb2.Result() - v1_parsed_result.ParseFromString(result.value) + v1_parsed_result = v1.program_pb2.Result.FromString( + result.value) self._results = self._get_job_results_v1(v1_parsed_result) elif (result_type == 'cirq.google.api.v2.Result' or result_type == 'cirq.api.google.v2.Result'): - v2_parsed_result = v2.result_pb2.Result() - v2_parsed_result.ParseFromString(result.value) + v2_parsed_result = v2.result_pb2.Result.FromString(result.value) self._results = self._get_job_results_v2(v2_parsed_result) else: raise ValueError( diff --git a/cirq/google/engine/engine_processor.py b/cirq/google/engine/engine_processor.py index d405443066fb..b49ca6da8b07 100644 --- a/cirq/google/engine/engine_processor.py +++ b/cirq/google/engine/engine_processor.py @@ -104,10 +104,8 @@ def get_device_specification( Device specification proto if present. """ if self._inner_processor().HasField('device_spec'): - device_spec = v2.device_pb2.DeviceSpecification() - device_spec.ParseFromString( + return v2.device_pb2.DeviceSpecification.FromString( self._inner_processor().device_spec.value) - return device_spec else: return None @@ -129,8 +127,8 @@ def get_device( @staticmethod def _to_calibration(calibration_any: qtypes.any_pb2.Any ) -> calibration.Calibration: - metrics = v2.metrics_pb2.MetricsSnapshot() - metrics.ParseFromString(calibration_any.value) + metrics = v2.metrics_pb2.MetricsSnapshot.FromString( + calibration_any.value) return calibration.Calibration(metrics) def list_calibrations(self, diff --git a/cirq/google/engine/engine_program.py b/cirq/google/engine/engine_program.py index b14635660194..bf90e18130e4 100644 --- a/cirq/google/engine/engine_program.py +++ b/cirq/google/engine/engine_program.py @@ -283,8 +283,7 @@ def _deserialize_program(code: qtypes.any_pb2.Any) -> 'Circuit': raise ValueError('deserializing a v1 Program is not supported') if (code_type == 'cirq.google.api.v2.Program' or code_type == 'cirq.api.google.v2.Program'): - program = v2.program_pb2.Program() - program.ParseFromString(code.value) + program = v2.program_pb2.Program.FromString(code.value) gate_set_map = { g.gate_set_name: g for g in gate_sets.GOOGLE_GATESETS } diff --git a/cirq/ion/ion_gates.py b/cirq/ion/ion_gates.py index 82593e464681..d91f694a61b4 100644 --- a/cirq/ion/ion_gates.py +++ b/cirq/ion/ion_gates.py @@ -14,12 +14,17 @@ """Operations native to iontrap systems.""" +from typing import Union, TYPE_CHECKING import numpy as np -from cirq import ops +from cirq import ops, value +from cirq import protocols +if TYPE_CHECKING: + import cirq -def ms(rads: float) -> ops.XXPowGate: + +class MSGate(ops.XXPowGate): """The Mølmer–Sørensen gate, a native two-qubit operation in ion traps. A rotation around the XX axis in the two-qubit bloch sphere. @@ -30,11 +35,42 @@ def ms(rads: float) -> ops.XXPowGate: [ 0 cos(t) -isin(t) 0 ] [ 0 -isin(t) cos(t) 0 ] [-isin(t) 0 0 cos(t) ] + """ + + def __init__( + self, + *, # Forces keyword args. + rads: float): + ops.XXPowGate.__init__(self, + exponent=rads * 2 / np.pi, + global_shift=-0.5) + + def _with_exponent(self: 'MSGate', exponent: value.TParamVal) -> 'MSGate': + return type(self)(rads=exponent * np.pi / 2) + + def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs' + ) -> Union[str, 'protocols.CircuitDiagramInfo']: + angle_str = self._format_exponent_as_angle(args, order=4) + symbol = f'MS({angle_str})' + return protocols.CircuitDiagramInfo(wire_symbols=(symbol, symbol)) + def __str__(self) -> str: + if self._exponent == 1: + return 'MS(π/2)' + return f'MS({self._exponent!r}π/2)' + + def __repr__(self) -> str: + if self._exponent == 1: + return 'cirq.ms(np.pi/2)' + return f'cirq.ms({self._exponent!r}*np.pi/2)' + + +def ms(rads: float) -> MSGate: + """ Args: rads: The rotation angle in radians. Returns: Mølmer–Sørensen gate rotating by the desired amount. """ - return ops.XXPowGate(exponent=rads * 2 / np.pi, global_shift=-0.5) + return MSGate(rads=rads) diff --git a/cirq/ion/ion_gates_test.py b/cirq/ion/ion_gates_test.py index 52ddcb10084e..979f330a6264 100644 --- a/cirq/ion/ion_gates_test.py +++ b/cirq/ion/ion_gates_test.py @@ -20,12 +20,17 @@ def test_ms_arguments(): eq_tester = cirq.testing.EqualsTester() eq_tester.add_equality_group(cirq.ms(np.pi / 2), - cirq.XXPowGate(global_shift=-0.5)) + cirq.ion.ion_gates.MSGate(rads=np.pi / 2)) + eq_tester.add_equality_group(cirq.XXPowGate(global_shift=-0.5)) def test_ms_str(): - assert str(cirq.ms(np.pi / 2)) == 'MS(π/2)' + ms = cirq.ms(np.pi / 2) + assert str(ms) == 'MS(π/2)' assert str(cirq.ms(np.pi)) == 'MS(2.0π/2)' + assert str(ms**0.5) == 'MS(0.5π/2)' + assert str(ms**2) == 'MS(2.0π/2)' + assert str(ms**-1) == 'MS(-1.0π/2)' def test_ms_matrix(): @@ -47,6 +52,9 @@ def test_ms_repr(): assert repr(cirq.ms(np.pi / 2)) == 'cirq.ms(np.pi/2)' assert repr(cirq.ms(np.pi / 4)) == 'cirq.ms(0.5*np.pi/2)' cirq.testing.assert_equivalent_repr(cirq.ms(np.pi / 4)) + ms = cirq.ms(np.pi / 2) + assert (repr(ms**2) == 'cirq.ms(2.0*np.pi/2)') + assert (repr(ms**-0.5) == 'cirq.ms(-0.5*np.pi/2)') def test_ms_diagrams(): diff --git a/cirq/ops/parity_gates.py b/cirq/ops/parity_gates.py index 8b022d5510b3..13b698b0b584 100644 --- a/cirq/ops/parity_gates.py +++ b/cirq/ops/parity_gates.py @@ -39,7 +39,7 @@ class XXPowGate(eigen_gate.EigenGate, [0 1 0 0] [1 0 0 0] - See also: `cirq.MS` (the Mølmer–Sørensen gate), which is implemented via + See also: `cirq.MSGate` (the Mølmer–Sørensen gate), which is implemented via this class. """ @@ -86,13 +86,6 @@ def _decompose_into_clifford_with_qubits_(self, qubits): def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs' ) -> Union[str, 'protocols.CircuitDiagramInfo']: - if self._global_shift == -0.5: - # Mølmer–Sørensen gate. - angle_str = self._format_exponent_as_angle(args, order=4) - symbol = f'MS({angle_str})' - return protocols.CircuitDiagramInfo( - wire_symbols=(symbol, symbol)) - return protocols.CircuitDiagramInfo( wire_symbols=('XX', 'XX'), exponent=self._diagram_exponent(args)) @@ -105,19 +98,11 @@ def _quil_(self, qubits: Tuple['cirq.Qid', ...], qubits[0], self._exponent, qubits[1]) def __str__(self) -> str: - if self._global_shift == -0.5: - if self._exponent == 1: - return 'MS(π/2)' - return f'MS({self._exponent!r}π/2)' if self.exponent == 1: return 'XX' return f'XX**{self._exponent!r}' def __repr__(self) -> str: - if self._global_shift == -0.5 and not protocols.is_parameterized(self): - if self._exponent == 1: - return 'cirq.ms(np.pi/2)' - return f'cirq.ms({self._exponent!r}*np.pi/2)' if self._global_shift == 0: if self._exponent == 1: return 'cirq.XX' diff --git a/cirq/ops/parity_gates_test.py b/cirq/ops/parity_gates_test.py index 16b9277814a3..74fa36baff05 100644 --- a/cirq/ops/parity_gates_test.py +++ b/cirq/ops/parity_gates_test.py @@ -63,23 +63,10 @@ def test_xx_str(): assert str(cirq.XX**0.5) == 'XX**0.5' assert str(cirq.XXPowGate(global_shift=0.1)) == 'XX' - ms = cirq.XXPowGate(global_shift=-0.5) - assert str(ms) == 'MS(π/2)' - assert str(ms**0.5) == 'MS(0.5π/2)' - assert str(ms**2) == 'MS(2.0π/2)' - assert str(ms**-1) == 'MS(-1.0π/2)' - - def test_xx_repr(): assert repr(cirq.XXPowGate()) == 'cirq.XX' assert repr(cirq.XXPowGate(exponent=0.5)) == '(cirq.XX**0.5)' - ms = cirq.XXPowGate(global_shift=-0.5) - assert (repr(ms) == 'cirq.ms(np.pi/2)') - assert (repr(ms**2) == 'cirq.ms(2.0*np.pi/2)') - assert (repr(ms**-0.5) == 'cirq.ms(-0.5*np.pi/2)') - - def test_xx_matrix(): np.testing.assert_allclose(cirq.unitary(cirq.XX), np.array([[0, 0, 0, 1], @@ -109,13 +96,12 @@ def test_xx_diagrams(): cirq.XX(a, b), cirq.XX(a, b)**3, cirq.XX(a, b)**0.5, - cirq.XXPowGate(global_shift=-0.5).on(a, b), ) cirq.testing.assert_has_diagram( circuit, """ -a: ───XX───XX───XX───────MS(0.5π)─── - │ │ │ │ -b: ───XX───XX───XX^0.5───MS(0.5π)─── +a: ───XX───XX───XX─────── + │ │ │ +b: ───XX───XX───XX^0.5─── """) diff --git a/docs/api.rst b/docs/api.rst index 8bcc6874d88a..afb9eee8f746 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -239,6 +239,7 @@ results. cirq.sample_sweep cirq.to_resolvers cirq.to_sweep + cirq.to_sweeps cirq.validate_mixture cirq.validate_probability cirq.xeb_fidelity @@ -349,6 +350,7 @@ the magic methods that can be implemented. cirq.resolve_parameters cirq.to_json cirq.trace_distance_bound + cirq.trace_distance_from_angle_list cirq.unitary cirq.ApplyChannelArgs cirq.ApplyMixtureArgs @@ -621,6 +623,7 @@ Algebra and Representation cirq.chosen_angle_to_canonical_half_turns cirq.chosen_angle_to_half_turns cirq.commutes + cirq.deconstruct_single_qubit_matrix_into_angles cirq.definitely_commutes cirq.diagonalize_real_symmetric_and_sorted_diagonal_matrices cirq.diagonalize_real_symmetric_matrix @@ -644,6 +647,7 @@ Algebra and Representation cirq.kron_with_controls cirq.map_eigenvalues cirq.match_global_phase + cirq.matrix_commutes cirq.matrix_from_basis_coefficients cirq.partial_trace cirq.partial_trace_of_state_vector_as_mixture @@ -675,6 +679,7 @@ Quantum Information Science cirq.one_hot cirq.to_valid_density_matrix cirq.to_valid_state_vector + cirq.validate_indices cirq.validate_normalized_state_vector cirq.validate_qid_shape cirq.von_neumann_entropy