I want to implement quantum version of Random Projection Dimensionality Reduction Technique on a dataset. Even after using the latest version of Qiskit, it gives the following error:

  Cell In[3], line 59
    main(csv_file, n_features_after_reduction)
  Cell In[3], line 48 in main
    reduced_features = quantum_random_projection(features, n_features_after_reduction)
  Cell In[3], line 21 in quantum_random_projection
    quantum_state = feature_map.encode(features)
AttributeError: 'ZZFeatureMap' object has no attribute 'encode'

The code I am using is:

import numpy as np
from qiskit import QuantumCircuit, transpile, execute, Aer
from qiskit.circuit.library import ZZFeatureMap

def load_dataset(csv_file):
    dataset = pd.read_csv(csv_file)
    labels = dataset.iloc[:, 0].to_numpy()
    features = dataset.iloc[:, 1:].to_numpy()
    return features, labels

def quantum_random_projection(features, n_features):

    feature_map = ZZFeatureMap(feature_dimension=n_features, reps=2, entanglement='linear', insert_barriers=False)
    quantum_state = feature_map.encode(features)
    random_circuit = QuantumCircuit(feature_map.num_qubits)
    random_circuit.cx(0, 1)

    combined_circuit = QuantumCircuit(feature_map.num_qubits)
    combined_circuit.append(quantum_state, 0)
    combined_circuit.append(random_circuit, 0)

    transpiled_circuit = transpile(combined_circuit, Aer.get_backend('statevector_simulator'))
    job = execute(transpiled_circuit, Aer.get_backend('statevector_simulator'), shots=8192)
    result = job.result()
    reduced_data = [result.get_counts(combined_circuit)[key] / 8192 for key in range(2 ** feature_map.num_qubits)]

    return reduced_data

def main(csv_file, n_features_after_reduction):

    features, labels = load_dataset(csv_file)
    reduced_features = quantum_random_projection(features, n_features_after_reduction)
    reduced_dataset = np.column_stack((reduced_features, labels))
    np.savetxt('reduced_dataset.csv', reduced_dataset, delimiter=',', fmt='%s')

if __name__ == "__main__":
    csv_file = 'initial.csv'
    n_features_after_reduction = 10
    main(csv_file, n_features_after_reduction)```

Also I am very new to quantum algorithms and have very little (tending to 0) knowledge about them.

I am using IBM Quantum learning platform to run my code.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.