18
18
Measurement ,
19
19
expand_operator ,
20
20
GATE_CLASS_MAP ,
21
- gate_sequence_product ,
22
21
)
23
22
from .circuitsimulator import (
24
23
CircuitSimulator ,
25
24
CircuitResult ,
26
25
)
27
- from qutip import basis , Qobj
26
+ from qutip import Qobj , qeye
28
27
29
28
30
29
try :
@@ -481,10 +480,6 @@ def run(
481
480
post-selection. If specified, the measurement results are
482
481
set to the tuple of bits (sequentially) instead of being
483
482
chosen at random.
484
- precompute_unitary: Boolean, optional
485
- Specify if computation is done by pre-computing and aggregating
486
- gate unitaries. Possibly a faster method in the case of
487
- large number of repeat runs with different state inputs.
488
483
489
484
Returns
490
485
-------
@@ -499,7 +494,6 @@ def run(
499
494
raise TypeError ("State is not a ket or a density matrix." )
500
495
sim = CircuitSimulator (
501
496
self ,
502
- U_list ,
503
497
mode ,
504
498
precompute_unitary ,
505
499
)
@@ -520,10 +514,6 @@ def run_statistics(
520
514
initialization of the classical bits.
521
515
U_list: list of Qobj, optional
522
516
list of predefined unitaries corresponding to circuit.
523
- precompute_unitary: Boolean, optional
524
- Specify if computation is done by pre-computing and aggregating
525
- gate unitaries. Possibly a faster method in the case of
526
- large number of repeat runs with different state inputs.
527
517
528
518
Returns
529
519
-------
@@ -537,12 +527,7 @@ def run_statistics(
537
527
mode = "density_matrix_simulator"
538
528
else :
539
529
raise TypeError ("State is not a ket or a density matrix." )
540
- sim = CircuitSimulator (
541
- self ,
542
- U_list ,
543
- mode ,
544
- precompute_unitary ,
545
- )
530
+ sim = CircuitSimulator (self , mode , precompute_unitary )
546
531
return sim .run_statistics (state , cbits )
547
532
548
533
def resolve_gates (self , basis = ["CNOT" , "RX" , "RY" , "RZ" ]):
@@ -892,48 +877,46 @@ def propagators(self, expand=True, ignore_measurement=False):
892
877
"Cannot compute the propagator of a measurement operator."
893
878
"Please set ignore_measurement=True."
894
879
)
895
-
896
880
for gate in gates :
897
881
if gate .name == "GLOBALPHASE" :
898
882
qobj = gate .get_qobj (self .N )
899
- U_list .append (qobj )
900
- continue
901
-
902
- if gate .name in self .user_gates :
903
- if gate .controls is not None :
904
- raise ValueError (
905
- "A user defined gate {} takes only "
906
- "`targets` variable." .format (gate .name )
907
- )
908
- func_or_oper = self .user_gates [gate .name ]
909
- if inspect .isfunction (func_or_oper ):
910
- func = func_or_oper
911
- para_num = len (inspect .getfullargspec (func )[0 ])
912
- if para_num == 0 :
913
- qobj = func ()
914
- elif para_num == 1 :
915
- qobj = func (gate .arg_value )
916
- else :
917
- raise ValueError (
918
- "gate function takes at most one parameters."
919
- )
920
- elif isinstance (func_or_oper , Qobj ):
921
- qobj = func_or_oper
922
- else :
923
- raise ValueError ("gate is neither function nor operator" )
883
+ else :
884
+ qobj = self ._get_gate_unitary (gate )
924
885
if expand :
925
886
all_targets = gate .get_all_qubits ()
926
887
qobj = expand_operator (
927
888
qobj , dims = self .dims , targets = all_targets
928
889
)
929
- else :
930
- if expand :
931
- qobj = gate .get_qobj (self .N , self .dims )
932
- else :
933
- qobj = gate .get_compact_qobj ()
934
890
U_list .append (qobj )
935
891
return U_list
936
892
893
+ def _get_gate_unitary (self , gate ):
894
+ if gate .name in self .user_gates :
895
+ if gate .controls is not None :
896
+ raise ValueError (
897
+ "A user defined gate {} takes only "
898
+ "`targets` variable." .format (gate .name )
899
+ )
900
+ func_or_oper = self .user_gates [gate .name ]
901
+ if inspect .isfunction (func_or_oper ):
902
+ func = func_or_oper
903
+ para_num = len (inspect .getfullargspec (func )[0 ])
904
+ if para_num == 0 :
905
+ qobj = func ()
906
+ elif para_num == 1 :
907
+ qobj = func (gate .arg_value )
908
+ else :
909
+ raise ValueError (
910
+ "gate function takes at most one parameters."
911
+ )
912
+ elif isinstance (func_or_oper , Qobj ):
913
+ qobj = func_or_oper
914
+ else :
915
+ raise ValueError ("gate is neither function nor operator" )
916
+ else :
917
+ qobj = gate .get_compact_qobj ()
918
+ return qobj
919
+
937
920
def compute_unitary (self ):
938
921
"""Evaluates the matrix of all the gates in a quantum circuit.
939
922
@@ -942,8 +925,9 @@ def compute_unitary(self):
942
925
circuit_unitary : :class:`qutip.Qobj`
943
926
Product of all gate arrays in the quantum circuit.
944
927
"""
945
- gate_list = self .propagators ()
946
- circuit_unitary = gate_sequence_product (gate_list )
928
+ sim = CircuitSimulator (self )
929
+ result = sim .run (qeye (self .dims ))
930
+ circuit_unitary = result .get_final_states ()[0 ]
947
931
return circuit_unitary
948
932
949
933
def latex_code (self ):
0 commit comments