14
14
15
15
"""Layers of Y rotations followed by entangling gates."""
16
16
17
+
17
18
from typing import Optional , List
18
19
import numpy as np
19
- from qiskit import QuantumRegister , QuantumCircuit
20
+ from qiskit . circuit . library import RY as RYCircuit
20
21
from qiskit .aqua .utils .validation import validate_min , validate_in_set
21
22
from qiskit .aqua .components .initial_states import InitialState
22
23
from .variational_form import VariationalForm
23
24
24
25
25
26
class RY (VariationalForm ):
26
- r"""
27
- The RY Variational Form.
27
+ r"""The RY Variational Form.
28
28
29
29
The RY trial wave function is layers of :math:`y` rotations with entanglements.
30
30
When none of qubits are unentangled to other qubits the number of parameters
@@ -95,7 +95,22 @@ def __init__(self,
95
95
validate_min ('depth' , depth , 1 )
96
96
validate_in_set ('entanglement' , entanglement , {'full' , 'linear' , 'sca' })
97
97
validate_in_set ('entanglement_gate' , entanglement_gate , {'cz' , 'cx' , 'crx' })
98
- super ().__init__ ()
98
+
99
+ # entangler map overrides entanglement
100
+ if entangler_map :
101
+ entanglement = entangler_map
102
+
103
+ ry_circuit = RYCircuit (num_qubits ,
104
+ reps = depth ,
105
+ entanglement = entanglement ,
106
+ entanglement_blocks = entanglement_gate ,
107
+ skip_unentangled_qubits = skip_unentangled_qubits ,
108
+ skip_final_rotation_layer = skip_final_ry ,
109
+ insert_barriers = True ,
110
+ initial_state = initial_state )
111
+
112
+ super ().__init__ (blueprint_circuit = ry_circuit )
113
+
99
114
self ._num_qubits = num_qubits
100
115
self ._depth = depth
101
116
self ._entanglement = entanglement
@@ -116,79 +131,6 @@ def __init__(self,
116
131
self ._skip_final_ry = skip_final_ry
117
132
118
133
# for the first layer
119
- self ._num_parameters = len (self ._entangled_qubits ) if self ._skip_unentangled_qubits \
120
- else self ._num_qubits
121
-
122
- # for repeated block (minus one ry layer if we skip the last)
123
- self ._num_parameters += len (self ._entangled_qubits ) * (depth - 1 ) if skip_final_ry \
124
- else len (self ._entangled_qubits ) * depth
125
-
126
- # CRx gates have an additional parameter per entanglement
127
- if entanglement_gate == 'crx' :
128
- self ._num_parameters += len (self ._entangler_map ) * depth
129
-
134
+ self ._num_parameters = ry_circuit .num_parameters
130
135
self ._bounds = [(- np .pi , np .pi )] * self ._num_parameters
131
136
self ._support_parameterized_circuit = True
132
-
133
- def construct_circuit (self , parameters , q = None ):
134
- """
135
- Construct the variational form, given its parameters.
136
-
137
- Args:
138
- parameters (Union(numpy.ndarray, list[Parameter], ParameterVector)): circuit parameters.
139
- q (QuantumRegister): Quantum Register for the circuit.
140
-
141
- Returns:
142
- QuantumCircuit: a quantum circuit with given `parameters`
143
-
144
- Raises:
145
- ValueError: the number of parameters is incorrect.
146
- """
147
- if len (parameters ) != self ._num_parameters :
148
- raise ValueError ('The number of parameters has to be {}' .format (self ._num_parameters ))
149
-
150
- if q is None :
151
- q = QuantumRegister (self ._num_qubits , name = 'q' )
152
- if self ._initial_state is not None :
153
- circuit = self ._initial_state .construct_circuit ('circuit' , q )
154
- else :
155
- circuit = QuantumCircuit (q )
156
-
157
- param_idx = 0
158
- for qubit in range (self ._num_qubits ):
159
- if not self ._skip_unentangled_qubits or qubit in self ._entangled_qubits :
160
- circuit .u3 (parameters [param_idx ], 0.0 , 0.0 , q [qubit ]) # ry
161
- param_idx += 1
162
-
163
- for block in range (self ._depth ):
164
- circuit .barrier (q )
165
- if self ._entanglement == 'sca' :
166
- self ._entangler_map = VariationalForm .get_entangler_map (
167
- self ._entanglement ,
168
- self ._num_qubits ,
169
- offset = block )
170
-
171
- for src , targ in self ._entangler_map :
172
- if self ._entanglement_gate == 'cz' :
173
- circuit .u2 (0.0 , np .pi , q [targ ]) # h
174
- circuit .cx (q [src ], q [targ ])
175
- circuit .u2 (0.0 , np .pi , q [targ ]) # h
176
-
177
- elif self ._entanglement_gate == 'crx' :
178
- circuit .cu3 (parameters [param_idx ], - np .pi / 2 , np .pi / 2 ,
179
- q [src ], q [targ ]) # crx
180
- param_idx += 1
181
-
182
- else :
183
- circuit .cx (q [src ], q [targ ])
184
-
185
- # Skip the final RY layer if it is specified and we reached the
186
- # last block
187
- if not self ._skip_final_ry or block != self ._depth - 1 :
188
- circuit .barrier (q )
189
- for qubit in self ._entangled_qubits :
190
- circuit .u3 (parameters [param_idx ], 0.0 , 0.0 , q [qubit ]) # ry
191
- param_idx += 1
192
- circuit .barrier (q )
193
-
194
- return circuit
0 commit comments