@@ -914,15 +914,8 @@ impl DAGCircuit {
914914 /// Args:
915915 /// angle (float, :class:`.ParameterExpression`): The phase angle.
916916 #[ setter( global_phase) ]
917- pub fn set_global_phase_param ( & mut self , angle : Param ) -> PyResult < ( ) > {
918- match angle {
919- Param :: Float ( angle) => self . set_global_phase_f64 ( angle) ,
920- Param :: ParameterExpression ( angle) => {
921- self . global_phase = Param :: ParameterExpression ( angle) ;
922- }
923- Param :: Obj ( _) => return Err ( PyTypeError :: new_err ( "Invalid type for global phase" ) ) ,
924- }
925- Ok ( ( ) )
917+ pub fn set_global_phase ( & mut self , angle : Param ) -> PyResult < ( ) > {
918+ self . set_global_phase_param ( angle) . map ( |_| ( ) )
926919 }
927920
928921 /// Remove all operation nodes with the given name.
@@ -6230,12 +6223,30 @@ impl DAGCircuit {
62306223 Ok ( ( in_node, out_node) )
62316224 }
62326225
6233- /// Set the global phase to a float value.
6226+ /// Set the global phase to a float value. Returns the old phase.
62346227 ///
62356228 /// Unlike the general [set_global_phase_param], this is infallible.
62366229 #[ inline]
6237- pub fn set_global_phase_f64 ( & mut self , angle : f64 ) {
6238- self . global_phase = Param :: Float ( angle. rem_euclid ( :: std:: f64:: consts:: TAU ) ) ;
6230+ pub fn set_global_phase_f64 ( & mut self , angle : f64 ) -> Param {
6231+ std:: mem:: replace (
6232+ & mut self . global_phase ,
6233+ Param :: Float ( angle. rem_euclid ( :: std:: f64:: consts:: TAU ) ) ,
6234+ )
6235+ }
6236+
6237+ /// Set the global phase to a general [Param]. Returns the old phase.
6238+ ///
6239+ /// Unlike the general [set_global_phase_param], this is infallible.
6240+ #[ inline]
6241+ pub fn set_global_phase_param ( & mut self , angle : Param ) -> PyResult < Param > {
6242+ match angle {
6243+ Param :: Float ( angle) => Ok ( self . set_global_phase_f64 ( angle) ) ,
6244+ Param :: ParameterExpression ( angle) => Ok ( std:: mem:: replace (
6245+ & mut self . global_phase ,
6246+ Param :: ParameterExpression ( angle) ,
6247+ ) ) ,
6248+ Param :: Obj ( _) => Err ( PyTypeError :: new_err ( "Invalid type for global phase" ) ) ,
6249+ }
62396250 }
62406251
62416252 /// Get the nodes on the given wire.
@@ -7298,14 +7309,13 @@ impl DAGCircuit {
72987309
72997310 pub fn add_global_phase ( & mut self , value : & Param ) -> PyResult < ( ) > {
73007311 match value {
7301- Param :: Obj ( _) => {
7302- return Err ( PyTypeError :: new_err (
7303- "Invalid parameter type, only float and parameter expression are supported" ,
7304- ) ) ;
7305- }
7306- _ => self . set_global_phase_param ( add_global_phase ( & self . global_phase , value ) ) ? ,
7312+ Param :: Obj ( _) => Err ( PyTypeError :: new_err (
7313+ "Invalid parameter type, only float and parameter expression are supported" ,
7314+ ) ) ,
7315+ _ => self
7316+ . set_global_phase_param ( add_global_phase ( & self . global_phase , value ) )
7317+ . map ( |_| ( ) ) ,
73077318 }
7308- Ok ( ( ) )
73097319 }
73107320
73117321 /// Return the op name counts in the circuit
0 commit comments