Skip to content

Commit

Permalink
Merge pull request qiskit-community/qiskit-aqua#693 from chunfuchen/p…
Browse files Browse the repository at this point in the history
…arameterized_circuits

Use parameterized circuits to speed up some algorithms instead of using CircuitCache
  • Loading branch information
woodsp-ibm authored Oct 1, 2019
2 parents b02f144 + 96ee7aa commit 4f55715
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
3 changes: 2 additions & 1 deletion qiskit/aqua/components/variational_forms/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ def __init__(self, num_qubits, depth=3, entangler_map=None,
self._num_parameters += len(self._entangler_map) * depth

self._bounds = [(-np.pi, np.pi)] * self._num_parameters
self._support_parameterized_circuit = True

def construct_circuit(self, parameters, q=None):
"""
Construct the variational form, given its parameters.
Args:
parameters (numpy.ndarray): circuit parameters.
parameters (Union(numpy.ndarray, list[Parameter], ParameterVector)): circuit parameters.
q (QuantumRegister): Quantum Register for the circuit.
Returns:
Expand Down
4 changes: 3 additions & 1 deletion qiskit/aqua/components/variational_forms/ryrz.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np
from qiskit import QuantumRegister, QuantumCircuit

from qiskit.aqua.components.variational_forms import VariationalForm


Expand Down Expand Up @@ -106,13 +107,14 @@ def __init__(self, num_qubits, depth=3, entangler_map=None,
# for repeated block
self._num_parameters += len(self._entangled_qubits) * depth * 2
self._bounds = [(-np.pi, np.pi)] * self._num_parameters
self._support_parameterized_circuit = True

def construct_circuit(self, parameters, q=None):
"""
Construct the variational form, given its parameters.
Args:
parameters (numpy.ndarray): circuit parameters
parameters (Union(numpy.ndarray, list[Parameter], ParameterVector)): circuit parameters
q (QuantumRegister): Quantum Register for the circuit.
Returns:
Expand Down
3 changes: 2 additions & 1 deletion qiskit/aqua/components/variational_forms/swaprz.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ def __init__(self, num_qubits, depth=3, entangler_map=None,
# for repeated block
self._num_parameters += (len(self._entangled_qubits) + len(self._entangler_map)) * depth
self._bounds = [(-np.pi, np.pi)] * self._num_parameters
self._support_parameterized_circuit = True

def construct_circuit(self, parameters, q=None):
"""
Construct the variational form, given its parameters.
Args:
parameters (numpy.ndarray): circuit parameters
parameters (Union(numpy.ndarray, list[Parameter], ParameterVector)): circuit parameters
q (QuantumRegister): Quantum Register for the circuit.
Returns:
Expand Down
15 changes: 15 additions & 0 deletions qiskit/aqua/components/variational_forms/variational_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
self._num_parameters = 0
self._num_qubits = 0
self._bounds = list()
self._support_parameterized_circuit = False
pass

@classmethod
Expand Down Expand Up @@ -75,6 +76,20 @@ def num_parameters(self):
"""
return self._num_parameters

@property
def support_parameterized_circuit(self):
""" Whether or not the sub-class support parameterized circuit.
Returns:
boolean: indicate the sub-class support parameterized circuit
"""
return self._support_parameterized_circuit

@support_parameterized_circuit.setter
def support_parameterized_circuit(self, new_value):
""" set whether or not the sub-class support parameterized circuit """
self._support_parameterized_circuit = new_value

@property
def num_qubits(self):
"""Number of qubits of the variational form.
Expand Down
13 changes: 3 additions & 10 deletions test/aqua/test_skip_qobj_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
""" Test Skip Qobj Validation """

import unittest
import os
from test.aqua.common import QiskitAquaTestCase
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import BasicAer
Expand Down Expand Up @@ -56,12 +55,10 @@ def setUp(self):

def test_wo_backend_options(self):
""" without backend options test """
os.environ.pop('QISKIT_AQUA_CIRCUIT_CACHE', None)
quantum_instance = QuantumInstance(self.backend,
seed_transpiler=self.random_seed,
seed_simulator=self.random_seed,
shots=1024,
circuit_caching=False)
shots=1024)
# run without backend_options and without noise
res_wo_bo = quantum_instance.execute(self.qc).get_counts(self.qc)

Expand All @@ -72,12 +69,10 @@ def test_wo_backend_options(self):
def test_w_backend_options(self):
""" with backend options test """
# run with backend_options
os.environ.pop('QISKIT_AQUA_CIRCUIT_CACHE', None)
quantum_instance = QuantumInstance(self.backend, seed_transpiler=self.random_seed,
seed_simulator=self.random_seed, shots=1024,
backend_options={
'initial_statevector': [.5, .5, .5, .5]},
circuit_caching=False)
'initial_statevector': [.5, .5, .5, .5]})
res_w_bo = quantum_instance.execute(self.qc).get_counts(self.qc)
quantum_instance.skip_qobj_validation = True
res_w_bo_skip_validation = quantum_instance.execute(self.qc).get_counts(self.qc)
Expand All @@ -95,16 +90,14 @@ def test_w_noise(self):
self.skipTest("Aer doesn't appear to be installed. Error: '{}'".format(str(ex)))
return

os.environ.pop('QISKIT_AQUA_CIRCUIT_CACHE', None)
probs_given0 = [0.9, 0.1]
probs_given1 = [0.3, 0.7]
noise_model = NoiseModel()
noise_model.add_readout_error([probs_given0, probs_given1], [0])

quantum_instance = QuantumInstance(self.backend, seed_transpiler=self.random_seed,
seed_simulator=self.random_seed, shots=1024,
noise_model=noise_model,
circuit_caching=False)
noise_model=noise_model)
res_w_noise = quantum_instance.execute(self.qc).get_counts(self.qc)

quantum_instance.skip_qobj_validation = True
Expand Down

0 comments on commit 4f55715

Please sign in to comment.