Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Commit b6d9801

Browse files
committed
make ry/ryrz use circlib
1 parent 3e14bb5 commit b6d9801

File tree

3 files changed

+37
-137
lines changed

3 files changed

+37
-137
lines changed

qiskit/aqua/algorithms/minimum_eigen_solvers/vqe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def _build_parameterized_circuits():
487487
# binding parameters here since the circuits had been transpiled
488488
if self._parameterized_circuits is not None:
489489
for idx, parameter in enumerate(parameter_sets):
490-
curr_param = {self._var_form_params: parameter}
490+
curr_param = dict(zip(self._var_form_params, parameter))
491491
for qc in self._parameterized_circuits:
492492
tmp = qc.bind_parameters(curr_param)
493493
tmp.name = str(idx) + tmp.name

qiskit/aqua/components/variational_forms/ry.py

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414

1515
"""Layers of Y rotations followed by entangling gates."""
1616

17+
1718
from typing import Optional, List
1819
import numpy as np
19-
from qiskit import QuantumRegister, QuantumCircuit
20+
from qiskit.circuit.library import RY as RYCircuit
2021
from qiskit.aqua.utils.validation import validate_min, validate_in_set
2122
from qiskit.aqua.components.initial_states import InitialState
2223
from .variational_form import VariationalForm
2324

2425

2526
class RY(VariationalForm):
26-
r"""
27-
The RY Variational Form.
27+
r"""The RY Variational Form.
2828
2929
The RY trial wave function is layers of :math:`y` rotations with entanglements.
3030
When none of qubits are unentangled to other qubits the number of parameters
@@ -95,7 +95,22 @@ def __init__(self,
9595
validate_min('depth', depth, 1)
9696
validate_in_set('entanglement', entanglement, {'full', 'linear', 'sca'})
9797
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+
99114
self._num_qubits = num_qubits
100115
self._depth = depth
101116
self._entanglement = entanglement
@@ -116,79 +131,6 @@ def __init__(self,
116131
self._skip_final_ry = skip_final_ry
117132

118133
# 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
130135
self._bounds = [(-np.pi, np.pi)] * self._num_parameters
131136
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

qiskit/aqua/components/variational_forms/ryrz.py

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
from typing import Optional, List
1818
import numpy as np
19-
from qiskit import QuantumRegister, QuantumCircuit
19+
from qiskit.circuit.library import RYRZ as RYRZCircuit
2020
from qiskit.aqua.utils.validation import validate_min, validate_in_set
2121
from qiskit.aqua.components.initial_states import InitialState
2222
from .variational_form import VariationalForm
2323

2424

2525
class RYRZ(VariationalForm):
26-
r"""
27-
The RYRZ Variational Form.
26+
r"""The RYRZ Variational Form.
2827
2928
The RYRZ trial wave function is layers of :math:`y` plus :math:`z` rotations with entanglements.
3029
When none of qubits are unentangled to other qubits, the number of optimizer parameters this
@@ -66,7 +65,19 @@ def __init__(self,
6665
validate_min('depth', depth, 1)
6766
validate_in_set('entanglement', entanglement, {'full', 'linear'})
6867
validate_in_set('entanglement_gate', entanglement_gate, {'cz', 'cx'})
69-
super().__init__()
68+
69+
if entangler_map:
70+
entanglement = entangler_map
71+
72+
ryrz = RYRZCircuit(num_qubits,
73+
entanglement_blocks=entanglement_gate,
74+
reps=depth,
75+
entanglement=entanglement,
76+
skip_unentangled_qubits=skip_unentangled_qubits,
77+
initial_state=initial_state)
78+
79+
super().__init__(blueprint_circuit=ryrz)
80+
7081
self._num_qubits = num_qubits
7182
self._depth = depth
7283
if entangler_map is None:
@@ -82,59 +93,6 @@ def __init__(self,
8293
self._entanglement_gate = entanglement_gate
8394
self._skip_unentangled_qubits = skip_unentangled_qubits
8495

85-
# for the first layer
86-
self._num_parameters = len(self._entangled_qubits) * 2 if self._skip_unentangled_qubits \
87-
else self._num_qubits * 2
88-
# for repeated block
89-
self._num_parameters += len(self._entangled_qubits) * depth * 2
96+
self._num_parameters = ryrz.num_parameters
9097
self._bounds = [(-np.pi, np.pi)] * self._num_parameters
9198
self._support_parameterized_circuit = True
92-
93-
def construct_circuit(self, parameters, q=None):
94-
"""
95-
Construct the variational form, given its parameters.
96-
97-
Args:
98-
parameters (Union(numpy.ndarray, list[Parameter], ParameterVector)): circuit parameters
99-
q (QuantumRegister): Quantum Register for the circuit.
100-
101-
Returns:
102-
QuantumCircuit: a quantum circuit with given `parameters`
103-
104-
Raises:
105-
ValueError: the number of parameters is incorrect.
106-
"""
107-
if len(parameters) != self._num_parameters:
108-
raise ValueError('The number of parameters has to be {}'.format(self._num_parameters))
109-
110-
if q is None:
111-
q = QuantumRegister(self._num_qubits, name='q')
112-
if self._initial_state is not None:
113-
circuit = self._initial_state.construct_circuit('circuit', q)
114-
else:
115-
circuit = QuantumCircuit(q)
116-
117-
param_idx = 0
118-
for qubit in range(self._num_qubits):
119-
if not self._skip_unentangled_qubits or qubit in self._entangled_qubits:
120-
circuit.u3(parameters[param_idx], 0.0, 0.0, q[qubit]) # ry
121-
circuit.u1(parameters[param_idx + 1], q[qubit]) # rz
122-
param_idx += 2
123-
124-
for _ in range(self._depth):
125-
circuit.barrier(q)
126-
for src, targ in self._entangler_map:
127-
if self._entanglement_gate == 'cz':
128-
circuit.u2(0.0, np.pi, q[targ]) # h
129-
circuit.cx(q[src], q[targ])
130-
circuit.u2(0.0, np.pi, q[targ]) # h
131-
else:
132-
circuit.cx(q[src], q[targ])
133-
circuit.barrier(q)
134-
for qubit in self._entangled_qubits:
135-
circuit.u3(parameters[param_idx], 0.0, 0.0, q[qubit]) # ry
136-
circuit.u1(parameters[param_idx + 1], q[qubit]) # rz
137-
param_idx += 2
138-
circuit.barrier(q)
139-
140-
return circuit

0 commit comments

Comments
 (0)