# Continuous emotional intensity controllable speech synthesis
using semi-supervised learning

###### Abstract

With the rapid development of the speech synthesis system, recent text-to-
speech models have reached the level of generating natural speech similar to
what humans say. But there still have limitations in terms of expressiveness.
In particular, the existing emotional speech synthesis models have shown
controllability using interpolated features with scaling parameters in
emotional latent space. However, the emotional latent space generated from the
existing models is difficult to control the continuous emotional intensity
because of the entanglement of features like emotions, speakers, etc. In this
paper, we propose a novel method to control the continuous intensity of
emotions using semi-supervised learning. The model learns emotions of
intermediate intensity using pseudo-labels generated from phoneme-level
sequences of speech information. An embedding space built from the proposed
model satisfies the uniform grid geometry with an emotional basis. In
addition, to improve the naturalness of intermediate emotional speech, a
discriminator is applied to the generation of low-level elements like
duration, pitch and energy. The experimental results showed that the proposed
method was superior in controllability and naturalness. The synthesized speech
samples are available at https://tinyurl.com/34zaehh2

Index Terms—  emotional speech synthesis, text-to-speech (TTS), semi-
supervised learning, emotional intensity control

##  1 Introduction

Recent advances in deep learning (DL) have significantly improved the
performance of speech synthesis systems, and the synthesized speech from DL-
based text-to-speech (TTS) models [1, 2, 3] have already shown excellent
performance about naturalness. It is suitable and sufficient for general
information delivery purposes to apply a speech synthesis system to real-world
applications. However, speech synthesis with conveying diverse emotions and
prosody differs significantly from actual human voices. Because it is
difficult to synthesize expressive speech including paralinguistic
characteristics such as pitch, stress, tone, and rhythm.

Expressive speech models are increasingly necessary, so emotional TTS research
is being aggressively pursued. There are several works [4, 5, 6, 7, 8, 9, 10,
11, 12] related to emotional speech synthesis model. First, some studies [4,
5, 6] proposed methods to extract emotional information from reference speech.
Global style token (GST) [4] demonstrated a style encoder trained by
unsupervised learning to extract style embedding vector from reference speech
and then exploited it to synthesize emotional speech. Other studies [5, 6]
used a speech emotion recognition (SER) model to learn a speech emotion
embedding space. Authors [7, 8] proposed a method to utilize categorical
emotion labels. Specifically, Lee et al [7] applied the emotion labels to the
attention RNN to enable emotional speech synthesis. Tits et al [8] fine-tuned
a pretrained speech synthesis model with a small set of emotional dataset.
Unfortunately, speech synthesized by the previous methods [4, 5, 6, 7]
provided only a coarse-grained expression because the entire sentence has been
adjusted with one global information. Therefore, it is difficult to reflect
the user’s requirements for fine-grained control in the emotional TTS model.

Fig. 1: The grid geometry with an emotional basis in the embedding space.
Embedding space of (a) conventional models and (b) the proposed method. Two
red points denote neutral and certain emotion. The yellow and white points are
the actual intermediate emotion and the linear interpolated emotion from the
two red points, respectively. Fig. 2:  Overall Architecture of proposed model.
$\mathcal{H}_{pho}$ is a hidden phoneme embedding and $\lambda$ is an
interpolation weight. (a) emotional speech synthesis framework based on
Fastspeech2 [3] (b) variance adaptor (c) speech mixer $M$ which is generating
mixed pitch $\tilde{p}_{(emo_{i},emo_{j})}^{\lambda}$, duration
$\tilde{d}_{(emo_{i},emo_{j})}^{\lambda}$, and energy
$\tilde{e}_{(emo_{i},emo_{j})}^{\lambda}$

To improve fine-grained expression, there are attempts to control an emotion
intensity [9, 10, 11, 12], not the categorical emotion of speech. [9, 10]
introduced models to reflect detailed emotional expression by adjusting
emotion strength with controllable parameter. [11] proposed a method to
control the intensity of emotions using non-linear interpolation from
categorical emotion embedding space. [12] controlled fine-grained emotion
intensity by conducting distance-based intensity quantization.

Even though authors [9, 10, 11, 12] have proposed controllable emotional
intensity models, there are two limitations. First, it is difficult to
synthesize speech by controlling the emotion space as desired. Conventional
emotional TTS models find the emotion embedding vector for discretized
intervals and utilize the vector to synthesize emotion speech. As shown in
Fig. 1(a), an embedding space is entangled not only with various emotions but
also with other features, like speaker identity, pitch or linguistic
information. Accordingly, the grid geometry from the perspective of the
emotional basis may form a valley-shaped grid as shown in Fig. 1(a). Due to
the valley-shaped gird in the embedding space, linearity for emotions cannot
be guaranteed, and it is hard to control emotions as desired. For example,
suppose you want to find an intermediate emotion (see the yellow point in Fig.
1) from two certain emotions (see red points in Fig. 1). If the embedding
space consists of the non-uniform grid as shown in Fig. 1(a), an emotion
predicted by interpolation models is far from the actual intermediate emotion
(see the white point in Fig. 1). Accordingly, the interpolated emotional
speech may be synthesized differently than desired. On the other hand, as
shown in Fig. 1(b), the predicted emotion is located close to the actual
intermediate emotion if the embedding space is disentangled, so that the
desired speech could be synthesized. Second, it is difficult to guarantee the
naturalness for intermediate emotional speech, because there are no loss
functions or constraints to improve the naturalness. Because of the
limitations mentioned above, it is a challenging task to generate the desired
speech from the grid of non-uniform emotional latent spaces.

In this work, we propose a method to continuously control the intensity of
emotion using semi-supervised learning. In order to learn the speech of
intermediate emotions, we propose a novel speech mixer, an augmentation method
to interpolate emotion labels and corresponding speech components (pitch,
duration and energy). Since the proposed model is directly trained with low-
level elements, more fine-grained embedding space can be constructed compared
to the conventional emotion latent space. As shown in Fig. 1(b), the emotion
embedding space is not corrupted by other features like speaker and
linguistic. In addition, a discriminator is applied to the variance adaptor
controlling duration, pitch and energy so that the model generates a more
realistic low-level element sequences [13].

Contributions in this study are as follows.

  * •

By using a novel low-level data mixer to generate intermediate emotion points,
the proposed model trained with semi-supervised learning can generate
emotional speech with a continuous intensity value.

  * •

By applying a discriminator to the variance adaptor, the mel-spectrogram can
be generated well without prediction loss.

Emotion | Method | (i) Emotion Intensity Recognition [%] | (ii) Speech Quality Evaluation
---|---|---|---
A $<$ B |  B $<$ C |  C $<$ D |  D $<$ E |  MOS $\uparrow$ |  MCD $\downarrow$ |  F0 RMSE $\downarrow$
(a) Happy |  Conventional [11] | 44.727 | 43.636 | 50.909 | 47.636 |  3.528$\pm$0.050 | 5.516 | 103.764
Proposed | 57.455 | 58.909 | 58.182 | 58.182 | 3.594$\pm$0.047 | 5.478 | 86.154
(b) Sad |  Conventional [11] | 46.545 | 44.364 | 40.364 | 44.364 |  3.509$\pm$0.052 | 5.691 | 82.523
Proposed | 57.818 | 55.273 | 59.636 | 58.182 | 3.654$\pm$0.045 | 5.470 | 76.468
(c) Angry |  Conventional [11] | 48.364 | 47.646 | 45.455 | 45.091 |  3.494$\pm$0.050 | 5.796 | 100.978
Proposed | 61.818 | 66.545 | 58.182 | 56.727 | 3.520$\pm$0.049 | 5.365 | 82.222
(d) Surprise |  Conventional [11] | 42.909 | 40.000 | 52.000 | 48.364 |  3.527$\pm$0.051 | 5.280 | 108.823
Proposed | 62.545 | 64.364 | 63.636 | 58.182 | 3.659$\pm$0.046 | 5.159 | 84.793
Table 1: Results of emotion intensity recognition and speech quality
evaluation. (i) Emotion Intensity Recognition is the recognition accuracy
between two speech samples of different intensity. (ii) Speech Quality
Evaluation denotes qualitative metric (MOS) and quantitative metrics (MCD and
F0 RMSE). MOS scores are presented with 95$\%$ confidence intervals. MCD and
F0 RMSE are evaluated for categorical emotion speech with ground-truth. A to E
represent emotional intensity, A = 0.00, B = 0.25, C = 0.50, D = 0.75, and E =
1.00.

##  2 Method

The overall architecture of the proposed model is shown in Fig. 2. Fastspeech2
[3] is used to generate a mel-spectrogram from the phoneme sequence. We
propose a speech mixer $M$ to generate pseudo-labels $\tilde{x}$ reflecting
intermediate emotion intensities in a variance adapter. The speech mixer $M$
generates an intermediate low-level elements like pitch $p$, duration $d$, and
energy $e$. Also, discriminators $D$ is applied to the predicted elements for
improving naturalness.

###  2.1 Speech Mixer

A speech mixer $M$ generates interpolated pseudo-labels $\tilde{x}$ for
intermediate emotion intensities. In order to interpolate any two emotions
$(emo_{i},emo_{j})$, emotion speech pair $(S_{emo_{i}},S_{emo_{j}})$ should be
sampled from different emotion categories
$\mathbb{E}=\\{emo_{1},emo_{2},...,emo_{K}\\}$ where $K$ denotes the number of
emotions. In this paper, we used $K=5$ and categorical emotions include
neutral, happy, sad, angry, and surprise. Its sampling function $F$ can be
represented by

$S_{emo_{j}}=F(S_{emo_{i}}).$

The emotion speech pair are sampled as follows

$\displaystyle(emo_{i}=neutral,~{}emo_{j}\in\mathbb{E}\setminus\\{neutral\\}),$
$\displaystyle(\text{resp.}~{}~{}~{}(emo_{i}\in\mathbb{E}\setminus\\{neutral\\},~{}emo_{j}=neutral)~{}~{}~{}).$

To generate a pseudo-label $\tilde{x}$, sampled pair
$(S_{emo_{i}},S_{emo_{j}})$ is converted into phoneme-level averaged values,
so that the same sentences have the same length of pitch
$(p_{emo_{i}},p_{emo_{j}})$, duration $(d_{emo_{i}},d_{emo_{j}})$ and energy
$(e_{emo_{i}},e_{emo_{j}})$. Then speech mixer $M$ generates pseudo-labels
$\tilde{x}_{(emo_{i},emo_{j})}^{\lambda}$ for intermediate intensity of
emotional speech, given by

$\displaystyle M(x_{emo_{i}},x_{emo_{j}},\lambda)$ $\displaystyle=g(\lambda
x_{emo_{i}}+(1-\lambda)x_{emo_{j}})$
$\displaystyle=\tilde{x}_{(emo_{i},emo_{j})}^{\lambda},$

where $x_{emo}\in\\{p_{emo},d_{emo},e_{emo}\\}$ and $\lambda$ denotes an
interpolation weight. $g(\cdot)$ denotes floor function if $x_{emo}=d_{emo}$
else identity function. Specifically, the interpolation weight $\lambda$ is
randomly selected from beta distribution $\beta(0.5,0.5)$. For notation
simplicity, we denote $x_{emo}=x$ and
$\tilde{x}_{(emo_{i},emo_{j})}^{\lambda}=\tilde{x}$.

###  2.2 Generator

As shown in Fig. 2(a), we use FastSpeech2 [3], which consists of a variance
adapter, phoneme-encoder, and decoder. The phoneme encoder receives a phoneme
sequence as an input and outputs an embedding vector. After adding a
positional encoding to the embedding vector, the encoder produces a hidden
phoneme embedding $\mathcal{H}_{pho}$.

Speaker and emotion Look-Up Tables (LUTs) are introduced to extend the
existing variance adapter to a multi-speaker setting like Fig. 2(b). The
speaker LUT is assigned to each speaker and trained to suit the speaker. The
emotion LUTs also are optimized according to the emotion labels. To optimize
the hidden phoneme embedding $\mathcal{H}_{pho}$, the speaker and the emotion
LUTs, loss functions for training each low-level element are described as
follows.

Loss of duration $\mathcal{L}_{d}$ consists of mean-square error (MSE) of
logarithm function such that

$\mathcal{L}_{d}=\mathop{\mathbb{E}[||log(d+1)-\hat{d}||_{2}]},$ (1)

where $d$ and $\hat{d}$ are a phoneme-level duration and its predicted value
from a duration predictor, respectively. Similar to loss of duration
$\mathcal{L}_{d}$, loss functions of pitch $\mathcal{L}_{p}$ and energy
$\mathcal{L}_{e}$ are formulated as MSE, given by

$\mathcal{L}_{p}=\mathop{\mathbb{E}[||p-\hat{p}||_{2}]},$ (2)
$\mathcal{L}_{e}=\mathop{\mathbb{E}[||e-\hat{e}||_{2}]},$ (3)

where $p$ and $e$ are labels of pitch and energy, respectively. $\hat{p}$ and
$\hat{e}$ denote predicted values from pitch and energy predictors. For Eqs.
(1), (2), and (3), labels $x\in\\{d,p,e\\}$ can be replaced with pseudo-labels
$\tilde{x}\in\\{\tilde{d},\tilde{p},\tilde{e}\\}$.

###  2.3 Discriminator

Low-level elements generated by the speech mixer do not exist a corresponding
speech ground-truth, so it is difficult to guarantee naturalness. Adversarial
training scheme is conducted to help the variance adaptor generate more
realistic pitch, duration and energy sequences. We adopt the least squares GAN
[14] loss for training our proposed model. Discriminators are shown as D in
Fig. 2(a), which are trained adversarially on the predicted pitch $\hat{p}$,
duration $\hat{d}$, and energy $\hat{e}$ from the variance adapter. The
adversarial loss $\mathcal{L}^{adv}_{x}$ is as follows:

$\mathcal{L}^{adv}_{x}=\mathop{\mathbb{E}[(x-1)^{2}]+\mathbb{E}[(\tilde{x})^{2}]}$
(4)

###  2.4 Training Objectives

Network training consists of two phases; (1) learning categorical emotion
using the original dataset $x$, (2) learning intermediate emotion using
pseudo-label data $\tilde{x}$ generated from a speech mixer $M$. First, when
the model is trained with a categorical dataset $x$, Eqs. (1), (2), and (3)
are used, and mean-absolute error (MAE) loss is also computed between a
ground-truth mel-spectrogram $y$ and predicted mel-spectrogram $\hat{y}$,
given by

$\mathcal{L}_{mel}=\mathop{\mathbb{E}[||y-\hat{y}||_{1}]}.$ (5)

So, categorical loss is defined as

$\mathcal{L}_{categorical}=\mathcal{L}_{mel}+\mathcal{L}_{p}+\mathcal{L}_{d}+\mathcal{L}_{e}.$

Second, when the network is trained with intermediate emotion $\tilde{x}$
generated from a speech mixer $M$, MSE losses are used similarly to a
categorical loss $\mathcal{L}_{categorical}$. However, the adversarial loss is
additionally applied to each pseudo-label $\tilde{x}$, instead of Eq. (5),
given by

$\mathcal{L}_{adv}=\mathcal{L}^{adv}_{p}+\mathcal{L}^{adv}_{d}+\mathcal{L}^{adv}_{e}.$
(6)

So, intermediate loss is defined as

$\mathcal{L}_{intermediate}=\mathcal{L}_{adv}+\mathcal{L}_{\tilde{p}}+\mathcal{L}_{\tilde{d}}+\mathcal{L}_{\tilde{e}}$

Finally, total training loss consists of categorical loss and intermediate
loss as follows

$\mathcal{L}_{total}=\mathcal{L}_{categorical}+\mathcal{L}_{intermediate}$

##  3 Experiments and Results

Proposed | Weight $\lambda$ | (i) Emotion Intensity Recognition [%] | (ii) Speech Quality Evaluation
---|---|---|---
A $<$ B |  B $<$ C |  C $<$ D |  D $<$ E |  MOS $\uparrow$ |  MCD $\downarrow$ |  F0 RMSE $\downarrow$
(a) w/o discriminator | Beta | 46.182 | 44.455 | 43.636 | 44.455 |  3.597$\pm$0.023 | 5.415 | 77.133
(b) w/ discriminator | Discrete | 54.091 | 50.818 | 50.818 | 53.273 |  3.602$\pm$0.023 | 5.337 | 79.359
Uniform | 43.455 | 41.455 | 40.000 | 41.455 |  3.589$\pm$0.024 | 5.367 | 79.562
Beta | 59.909 | 61.273 | 59.909 | 57.818 | 3.607$\pm$0.045 | 5.362 | 82.409
Table 2:  Ablation study of discriminator and interpolation weight $\lambda$.
Scores are average of all emotions. Discrete means that the data mixing ratio
is randomly selected from 0, 0.5, or 1.0. Uniform means that the ratio is
sampled from the uniform (0,1) distribution. Fig. 3: Plotting pitch contours
of (a) conventional method [11] and (b) proposed method according to emotional
intensity.

###  3.1 Dataset

We used Emotional Speech Database (ESD) [15] for multi-speaker models. The ESD
covers five emotions (neutral, happy, angry, sad and surprise) and comprises
of 350 parallel utterances from 10 native English speakers and 10 native
Chinese speakers. We only used the English dataset with all emotions for
training and evaluation.

###  3.2 Model Performance

To evaluate the emotion intensity recognition of the proposed model, a
preference test was conducted. We used a crowd-sourced platform (Amazon
Mechanical Turk) for the listening test, and 11 sentences were randomly
sampled for each emotion. A total of 220 people participated in the
experiment. First, the raters listen to the same speaker and speech uttered
with a neutral emotion, and speech uttered with a specific emotion as a
reference. Then, two sentences uttered with different intensities are given,
and among the two sentences, raters should select the one with the stronger
emotion. A specific emotion is one of four emotions like happy, sad, angry, or
surprise, and 4 intensity types were tested. There are 4 types such as (0.0 vs
0.25), (0.25 vs 0.5), (0.5 vs 0.75), and (0.75 vs 1.0). For speech quality
evaluation, mean opinion score (MOS) [16] was measured through a questionnaire
to verify the speech naturalness. For categorical emotional speech, mel
cepstral distortion (MCD) [17] and F0 root mean square error (F0 RMSE) were
computed for quantitative evaluation. Conventional method [11] controls
emotion intensity through non-linear interpolation based on GST [4]. As shown
in Table 1, the proposed method outperforms the conventional model [11] in all
metrics. Specifically, Table 1(i) shows that our proposed method achieves the
best accuracy for all intensity types. This indicates that the proposed model
can synthesize speech well according to the given intensity scale. In
addition, in the case of speech quality evaluation, the proposed method showed
better performance than the conventional model [11] in all emotions as shown
in Table 1(ii).

###  3.3 Ablation Study

We conducted an ablation study to validate the effectiveness of the
discriminator. In the proposed model w/o discriminator at Table 2(a), all
types of emotion intensity accuracy decreased compared to the model w/
discriminator when $\lambda$ distribution is beta (see Table 2(i)). However,
for the F0 RMSE metric as shown in Table 2(ii), the model w/o discriminator
represented better performance than w/ discriminator since the model w/o
discriminator was only optimized to minimize regression losses related to
labels and pseudo-labels.

In addition, another ablation study was conducted for different interpolation
weight distributions of speech mixer $M$. We compared discrete and uniform
distributions as interpolation weight $\lambda$. Discrete distribution means
that the mixing ratio $\lambda$ is randomly sampled from among 0, 0.5, and
1.0. And uniform means that the ratio $\lambda$ is sampled from the uniform
distribution $\mathcal{U}(0,1)$. The proposed model trained with the speech
mixer using beta distribution $\beta(0.5,0.5)$ shows the best performance of
the emotion intensity recognition as shown in Table 2(a)(i). However, the
model with discrete distribution achieved the best MCD and F0 RMSE scores
except w/o discriminator (see Table 2(b)(ii)). The model trained with the
discrete distribution can frequently encounter categorical labels and be
optimized, thus the quantitative metrics are minimized.

###  3.4 Plotting pitch contours of samples

Synthesized speech samples of the proposed model and conventional model [11]
were analyzed. The pitch contour was plotted for the same speaker and sentence
as shown in Fig. 3. The pitch contour of the proposed model dynamically
changed according to the emotional intensity $\lambda$. However, the
conventional model [11] showed similar pitch contours despite the intensity
$\lambda$ being modified from 0.25 to 0.75. In particular, the proposed model
can synthesize the speech at any emotional intensity (see the dashed line in
Fig. 3(b)) although the conventional model [11] cannot (see Fig. 3(a)). It
means that the pitch sequences can be controlled by selecting the desired
intensity with any continuous value. Thus, we confirmed that our proposed
model can dynamically adjust the intensity of emotions.

##  4 Conclusion

Improving expression in the domain of speech synthesis is very important but
challenging task. In particular, for supervised learning, labeling a dataset
that can control the emotions of speech is a laborious and difficult task.
Therefore, we proposed a model that can control the emotional intensity with
continuous value using semi-supervised learning. Intermediate low-level
elements are generated for a categorical emotional speech dataset, and it is
used as a pseudo-label for network learning. Also, the ground-truth mel-
spectrogram does not exist in the pseudo-labels, so a discriminator is used to
supplement it. The proposed model through experiments showed superior
performance in emotional intensity control and naturalness.

## References

  * [1] Xu Tan, Jiawei Chen, Haohe Liu, Jian Cong, Chen Zhang, Yanqing Liu, Xi Wang, Yichong Leng, Yuanhao Yi, Lei He, et al.,  “Naturalspeech: End-to-end text to speech synthesis with human-level quality,”  arXiv preprint arXiv:2205.04421, 2022.
  * [2] Jaehyeon Kim, Jungil Kong, and Juhee Son,  “Conditional variational autoencoder with adversarial learning for end-to-end text-to-speech,”  in International Conference on Machine Learning. PMLR, 2021, pp. 5530–5540.
  * [3] Yi Ren, Chenxu Hu, Xu Tan, Tao Qin, Sheng Zhao, Zhou Zhao, and Tie-Yan Liu,  “Fastspeech 2: Fast and high-quality end-to-end text to speech,”  arXiv preprint arXiv:2006.04558, 2020.
  * [4] Yuxuan Wang, Daisy Stanton, Yu Zhang, RJ-Skerry Ryan, Eric Battenberg, Joel Shor, Ying Xiao, Ye Jia, Fei Ren, and Rif A Saurous,  “Style tokens: Unsupervised style modeling, control and transfer in end-to-end speech synthesis,”  in International Conference on Machine Learning. PMLR, 2018, pp. 5180–5189.
  * [5] Pengfei Wu, Zhenhua Ling, Lijuan Liu, Yuan Jiang, Hongchuan Wu, and Lirong Dai,  “End-to-end emotional speech synthesis using style tokens and semi-supervised training,”  in 2019 Asia-Pacific Signal and Information Processing Association Annual Summit and Conference (APSIPA ASC). IEEE, 2019, pp. 623–627.
  * [6] Xiong Cai, Dongyang Dai, Zhiyong Wu, Xiang Li, Jingbei Li, and Helen Meng,  “Emotion controllable speech synthesis using emotion-unlabeled dataset with the assistance of cross-domain speech emotion recognition,”  in ICASSP 2021-2021 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP). IEEE, 2021, pp. 5734–5738.
  * [7] Younggun Lee, Azam Rabiee, and Soo-Young Lee,  “Emotional end-to-end neural speech synthesizer,”  arXiv preprint arXiv:1711.05447, 2017.
  * [8] Noé Tits, Kevin El Haddad, and Thierry Dutoit,  “Exploring transfer learning for low resource emotional tts,”  in Proceedings of SAI Intelligent Systems Conference. Springer, 2019, pp. 52–60.
  * [9] Tao Li, Shan Yang, Liumeng Xue, and Lei Xie,  “Controllable emotion transfer for end-to-end speech synthesis,”  in 2021 12th International Symposium on Chinese Spoken Language Processing (ISCSLP). IEEE, 2021, pp. 1–5.
  * [10] Tao Li, Xinsheng Wang, Qicong Xie, Zhichao Wang, and Lei Xie,  “Controllable cross-speaker emotion transfer for end-to-end speech synthesis,”  arXiv preprint arXiv:2109.06733, 2021.
  * [11] Se-Yun Um, Sangshin Oh, Kyungguen Byun, Inseon Jang, ChungHyun Ahn, and Hong-Goo Kang,  “Emotional speech synthesis with rich and granularized control,”  in ICASSP 2020-2020 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP). IEEE, 2020, pp. 7254–7258.
  * [12] Chae-Bin Im, Sang-Hoon Lee, Seung-Bin Kim, and Seong-Whan Lee,  “Emoq-tts: Emotion intensity quantization for fine-grained controllable emotional text-to-speech,”  in ICASSP 2022-2022 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP). IEEE, 2022, pp. 6317–6321.
  * [13] Sang-Hoon Lee, Hyun-Wook Yoon, Hyeong-Rae Noh, Ji-Hoon Kim, and Seong-Whan Lee,  “Multi-spectrogan: High-diversity and high-fidelity spectrogram generation with adversarial style combination for speech synthesis,”  in Proceedings of the AAAI Conference on Artificial Intelligence, 2021, vol. 35, pp. 13198–13206.
  * [14] Xudong Mao, Qing Li, Haoran Xie, Raymond YK Lau, Zhen Wang, and Stephen Paul Smolley,  “Least squares generative adversarial networks,”  in Proceedings of the IEEE international conference on computer vision, 2017, pp. 2794–2802.
  * [15] Kun Zhou, Berrak Sisman, Rui Liu, and Haizhou Li,  “Seen and unseen emotional style transfer for voice conversion with a new emotional speech dataset,”  in ICASSP 2021-2021 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP). IEEE, 2021, pp. 920–924.
  * [16] Robert C Streijl, Stefan Winkler, and David S Hands,  “Mean opinion score (mos) revisited: methods and applications, limitations and alternatives,”  Multimedia Systems, vol. 22, no. 2, pp. 213–227, 2016.
  * [17] Robert Kubichek,  “Mel-cepstral distance measure for objective speech quality assessment,”  in Proceedings of IEEE pacific rim conference on communications computers and signal processing. IEEE, 1993, vol. 1, pp. 125–128.


# Computing the strong metric dimension for
co-maximal ideal graphs of commutative rings††thanks: Key Words: Strong metric
dimension, Strong resolving set, Co-maximal ideal graph.
2010 Mathematics Subject Classification: 13A99; 05C78; 05C12.

R. Shariyaria, R. Nikandishb,c, A. Tehraniana, H. Rasoulia
aDepartment of Mathematics, Science and Research Branch,
Islamic Azad University, Tehran, Iran
bDepartment of Mathematics, Jundi-Shapur University of Technology, Dezful,
Iran
cJundi-Shapur Research Institute, Jundi-Shapur University of Technology,
Dezful, Iran

<EMAIL_ADDRESS><EMAIL_ADDRESS><EMAIL_ADDRESS><EMAIL_ADDRESS>Corresponding author

###### Abstract

Let $R$ be a commutative ring with identity. The co-maximal ideal graph of
$R$, denoted by $\Gamma(R)$, is a simple graph whose vertices are proper
ideals of $R$ which are not contained in the Jacobson radical of $R$ and two
distinct vertices $I,J$ are adjacent if and only if $I+J=R$. In this paper, we
use Gallai${}^{{}^{,}}$s Theorem and the concept of strong resolving graph to
compute the strong metric dimension for co-maximal ideal graphs of commutative
rings. Explicit formulae for the strong metric dimension, depending on whether
the ring is reduced or not, are established.

##  1 Introduction

The concept of metric dimension which enables an observer to uniquely
recognize the current position of a moving enemy in a network was initiated by
Harary and Melter [11]. This parameter has found various applications in the
other fields of sciences (see for instance [7, 15]). From then on many graph
theorists have been attracted by computing the metric dimension of graphs (see
for example [6, 12, 14, 16]). In 2004, Sebö and Tannier ([26]) introduced a
more restricted parameter than metric dimension called strong metric
dimension. Computing the strong metric dimension of graphs has appeared in
some publications (see [17, 18, 24] for more information). Both of the strong
background and wide range of applications motivate some of algebraic graph
theorists to study metric and strong metric dimensions of graphs associated
with algebraic structures, see [5, 8, 9, 10, 13, 19, 20, 23, 25]. In this
paper, we deal with the problem of finding the strong metric dimension for co-
maximal graph associated with a commutative ring.

Throughout this paper, all rings $R$ are commutative with identity. The sets
of all maximal ideals, ideals with non-zero annihilators, Jacobson radical and
nilpotent elements of $R$ are denoted by $\mathrm{Max}(R)$, $A(R)$, $J(R)$,
$Nil(R)$, respectively. If $T$ is a subset of a ring $R$, then the symbol
$T^{*}$ denotes $T\setminus\\{0\\}$. Moreover, $Ann_{R}(T)=\\{r\in
R|\,rT=0\\}$. The ring $R$ is called reduced if $Nil(R)=\\{0\\}$.

By $G=(V,E)$, we mean a simple and undirected graph $G$ with the vertex set
$V=V(G)$ and edge set $E=E(G)$. A connected graph is a graph in which there
exists at least one path between any two vertices. Distance between two
distinct vertices $x,y$, denoted by $d(x,y)$, is the length of the shortest
path between $x$ and $y$ and diam$(G)=max\\{d(x,y)\,|x,y\in V\\}$ is called
the $\it diameter$ of $G$. In the graph $G$, let $V_{0}\subseteq V$ and
$E_{0}\subseteq E$. The induced subgraph by $V_{0}$, denoted by $G[V_{0}]$, is
a subgraph of $G$ whose vertex set and edge set are $V_{0}$ and
$E_{0}=\\{\\{u,v\\}\in E\,|\,u,v\in V_{0}\\}$, respectively. Let $x\in V$.
Then the open and closed neighborhood of $x$ are denote by $N(x)$ and $N[x]$,
respectively. A $\it complete$ graph is a graph in which each pair of vertices
are adjacent. We use $K_{n}$ to denote a complete graph of order $n$. A set
$S$ of vertices of a graph $G$ is a vertex cover of $G$ if every edge of $G$
has one end in $S$. The vertex cover number of $G$, denoted by $\alpha(G)$, is
the smallest cardinality of a vertex cover of $G$. The independence number of
a graph $G$, denoted by $\beta(G)$, is the largest cardinality of an
independent set. For a graph $G$, $S\subseteq V(G)$ is called a $\it clique$
if the induced subgraph on $S$ is complete. The number of vertices in the
largest clique of a graph $G$ is called the $\it clique\,\,number$ of $G$ and
denoted by $\omega(G)$. For a connected graph $G$, let
$S=\\{v_{1},v_{2},\dots,v_{k}\\}$ be an ordered subset of $V(G)$ and $v\in
V(G)\setminus S$. The metric representation of $v$ with respect to $S$ is the
$k$-vector $D(v|S)=(d(v,v_{1}),d(v,v_{2}),\dots,d(v,v_{k}))$. For $S\subseteq
V(G)$, if for every $u,v\in V(G)\setminus S$, $D(u|S)=D(v|S)$ implies that
$u=v$, then $S$ is called a resolving set for $G$. The metric basis for $G$ is
a resolving set $S$ of minimum cardinality and the number of elements in $S$
is called the metric dimension of $G$ and denoted by $dim_{M}(G)$. A vertex
$w$ of a connected graph $G$ strongly resolves two vertices $u,v$ of $G$ if
there exists a shortest path from $u$ to $w$ containing $v$ or a shortest path
from $v$ to $w$ containing $u$. A set $S$ of vertices is a strong resolving
set for $G$ if every pair of vertices of $G$ is strongly resolved by some
vertex of $S$. The smallest cardinality of a strong resolving set for $G$ is
called the strong metric dimension of $G$ and denoted by $sdim_{M}(G)$. For
all undefined notions from graph theory, we refer the reader to [28].

Let $R$ be a ring. The co-maximal ideal graph of $R$, denoted by $\Gamma(R)$,
is a graph whose vertices are proper ideals of $R$ which are not contained in
the Jacobson radical of $R$ and two distinct vertices $I,J$ are adjacent if
and only if $I+J=R$. The concept of co-maximal ideal graph of a commutative
ring was first introduced and studied in [30]. Since then co-maximal ideal
graphs of commutative rings have been studied by several authors, for instance
see [2, 29, 31]. It is worth mentioning that co-maximal graphs for lattices in
[1], for groups in [3], for matrix algebras in [21], for two generated groups
in [22] and for non-commutative rings in [27] were investigated. This paper is
devoted to study the strong metric dimension of a co-maximal ideal graph and
it is organized as follows. In Section 2, we completely determine
$sdim_{M}(\Gamma(R))$ in terms of the number of maximal ideals of $R$, in case
$R$ is reduced. In Section 3, we focus on the strong metric dimension of
$\Gamma(R)$, when $R$ is a non-reduced ring.

##  2 Reduced rings case

In this section, we present strong metric dimension formula for a co-maximal
ideal graph, when $R$ is reduced. We begin with a series of lemmas.

###### Lemma 2.1

Let $G$ be a connected graph and $diam(G)=d<\infty$. Then the following
statements hold.

$(1)$ $dim_{M}(G)$ is finite if and only if $G$ is finite.

$(2)$ If $W\subset V(G)$ is a strong resolving set of $G$ and $u,v\in V(G)$
such that $N(u)=N(v)$ or $N[u]=N[v]$, then $u\in W$ or $v\in W$.

$(3)$ If $W\subset V(G)$ is a strong resolving set of $G$ and $u,v\in V(G)$
such that $d(u,v)=diam(G)$, then $u\in W$ or $v\in W$.

###### Proof.

$(1)$ To prove the non-trivial direction, assume that $dim_{M}(G)$ is finite
and for some non-negative integer $n$, let $W=\\{I_{1},I_{2},\dots,I_{n}\\}$
be a metric basis for $G$. Since $diam(G)=d<\infty$, for every $x\in
V(G)\setminus W$, there are only $(d+1)^{n}$ choices for $D(x|W)$. Thus
$|V(G)|\leq(d+1)^{n}+n$ and hence $G$ is finite.

$(2)$ and $(3)$ are obvious.  $\Box$

Let $G$ be a graph. It is easily seen that every strong resolving set is also
a resolving set, which leads to $dim_{M}(G)\leq sdim_{M}(G)$. Hence, we have
the following immediate corollary.

###### Corollary 2.1

Let $R$ be a ring. Then $sdim_{M}(\Gamma(R))$ is finite if and only if
$\Gamma(R)$ is finite.

The following well-known result, due to Gallai, which states a relationship
between the independence number and the vertex cover number of a graph $G$ has
a key role in this paper.

###### Lemma 2.2

(Gallai${}^{{}^{,}}$s Theorem) For any graph $G$ of order $n$,
$\alpha(G)+\beta(G)=n$.

A vertex $u$ of $G$ is maximally distant from $v$ (in $G$) if for every $w\in
N(u)$, $d(v,w)\leq d(u,v)$. If $u$ is maximally distant from $v$ and $v$ is
maximally distant from $u$, then we say that $u$ and $v$ are mutually
maximally distant. The boundary of $G$ is defined as

$\partial(G)=\\{u\in V(G)|\,\,{\rm there\,\,exists}\,v\in V(G)\,{\rm
such\,\,that}\,\,u,v\,\,{\rm are\,mutually\,maximally\,distant}\\}$.

We use the notion of strong resolving graph introduced by Oellermann and
Peters-Fransen in [24]. The strong resolving graph of $G$ is a graph $G_{SR}$
with vertex set $V(G_{SR})=\partial(G)$ where two vertices $u,v$ are adjacent
in $G_{SR}$ if and only if $u$ and $v$ are mutually maximally distant.

It was shown in [24] that the problem of finding the strong metric dimension
of a graph $G$ can be transformed into the problem of computing the vertex
cover number of $G_{SR}$.

###### Lemma 2.3

([24, Theorem 2.1]) For any connected graph $G$, $sdim_{M}(G)=\alpha(G_{SR})$.

The next example illustrates the validity of Lemma 2.3.

###### Example 2.1

$(1)$ Since $(K_{n})_{SR}=K_{n}$, $sdim_{M}(K_{n})=n-1$.

$(2)$ Let $R=\mathbb{Z}_{2}\times\mathbb{Z}_{2}\times\mathbb{Z}_{2}$. Suppose
that
$X=\\{(\mathbb{Z}_{2},\mathbb{Z}_{2},0),(0,\mathbb{Z}_{2},\mathbb{Z}_{2}),(\mathbb{Z}_{2},0,\mathbb{Z}_{2})\\}$
and $Y=\\{(\mathbb{Z}_{2},0,0),(0,\mathbb{Z}_{2},0),(0,0,\mathbb{Z}_{2})\\}$.
It is not hard to see that for any $u\in X$, there is no $v\in V(\Gamma(R))$
such that $u$ and $v$ are mutually maximally distant, whereas each pair of
vertices in $Y$ are mutually maximally distant. This follows that
$\partial(\Gamma(R))=\\{(\mathbb{Z}_{2},0,0),(0,\mathbb{Z}_{2},0),(0,0,\mathbb{Z}_{2})\\}$
and $\Gamma(R)_{SR}=K_{3}$. Since $\alpha(\Gamma(R)_{SR})=2$, Lemma 2.3
follows that $sdim_{M}(\Gamma(R))=2$. On the other hand,
$W=\\{(\mathbb{Z}_{2},0,0),(0,\mathbb{Z}_{2},0)\\}$ is the minimum strong
resolving set, i.e., $sdim_{M}(\Gamma(R))=2$.

$(\mathbb{Z}_{2},0,0)$$(0,\mathbb{Z}_{2},\mathbb{Z}_{2})$$(\mathbb{Z}_{2},\mathbb{Z}_{2},0)$$(\mathbb{Z}_{2},0,\mathbb{Z}_{2})$$(0,0,\mathbb{Z}_{2})$$(0,\mathbb{Z}_{2},0)$$(\mathbb{Z}_{2},0,0)$$(0,0,\mathbb{Z}_{2})$$(0,\mathbb{Z}_{2},0)$$\Gamma(R)$$\Gamma(R)_{SR}$

It was proved in [30, Theorem 2.4] that diam($\Gamma(R))\leq 3$. Hence we omit
the elementary proof of the next lemma.

###### Lemma 2.4

Let $R\cong F_{1}\times\cdots\times F_{n}$, where $F_{i}$ is a field for every
$1\leq i\leq n$ and let $I$, $J$ be two vertices of $\Gamma(R)$. Then the
following statements hold.

$(1)$ $d(I,J)_{\Gamma(R)}=2$ if and only if $I\cap J\neq 0$ and $I+J\neq R$.

$(2)$ $d(I,J)_{\Gamma(R)}=3$ if and only if $I\cap J=0$ and $I+J\neq R$.

$(3)$ If $I\subseteq J$ or $J\subseteq I$, or $I+J=R$, then $I$ and $J$ are
not mutually maximally distant.

In order to present our results we need to introduce some more terminologies.

Let $R$ be a ring and $\Gamma(R)$ be the co-maximal ideal graph of $R$. We
define $\Gamma(R)^{**}$ as the graph with vertex set
$V(\Gamma(R)^{**})=V(\Gamma(R))$ such that two vertices $I,J$ are adjacent in
$\Gamma(R)^{**}$ if and only if $I+J\neq R$, $I\nsubseteq J$ and $J\nsubseteq
I$. Also, we define $\Gamma(R)^{*}$ as follows: Let $\Gamma(R)^{*}=\Gamma(R)$,
if $\Gamma(R)$ is complete, and otherwise $\Gamma(R)^{*}$ is obtained from
$\Gamma(R)^{**}$ by removing all its isolated vertices.

Let $R\cong R_{1}\times\cdots\times R_{n}$, where $R_{i}$ is a ring for every
$1\leq i\leq n$. Let $I=(I_{1},I_{2},\dots,I_{n})$ be an ideal of $R$. By
$NZC(I)$, we mean the number of nonzero components of $I$.

###### Lemma 2.5

Let $R$ be a reduced ring and $sdim_{M}(\Gamma(R))<\infty$. Then the following
statements hold.

$(1)$ If $|\mathrm{Max}(R)|=2$, then
$\Gamma(R)^{**}=\Gamma(R)^{*}=\Gamma(R)=\Gamma(R)_{SR}=K_{2}$.

$(2)$ If $|\mathrm{Max}(R)|\geq 3$, then $I$ is an isolated vertex in
$\Gamma(R)^{**}$ if and only if $I\in\mathrm{Max}(R)$.

$(3)$ If $|\mathrm{Max}(R)|=n\geq 3$, then
$\Gamma(R)^{**}=H+K_{1}+K_{1}+\cdots+K_{1}$ ($n$ times), where $H$ is a
connected graph.

$(4)$ $\Gamma(R)^{*}=\Gamma(R)_{SR}$.

###### Proof.

$(1)$ Since $sdim_{M}({\Gamma}(R))$ is finite, $R$ has finitely many ideals,
by Corollary 2.1 and so [4, Theorem 8.7] implies that $R\cong
F_{1}\times\cdots\times F_{n}$, where $F_{i}$ is a field for every $1\leq
i\leq n=|\mathrm{Max}(R)|$. If $n=2$, since $\Gamma(R)=K_{2}$, we have
$\Gamma(R)^{**}=\Gamma(R)^{*}=\Gamma(R)=\Gamma(R)_{SR}=K_{2}$.

$(2)$ First we show that $I$ is an isolated vertex in $\Gamma(R)^{**}$, for
every $I\in\mathrm{Max}(R)$. Assume that $I$ is adjacent to $J$ in
$\Gamma(R)^{**}$. Then $I\nsubseteq J$ and $J\nsubseteq I$. Hence $I+J=R$, a
contradiction. Let $A=V(\Gamma(R)^{**})\setminus\mathrm{Max}(R)$. To complete
the proof, we show that $\Gamma(R)^{**}[A]$ is a connected graph. To see this,
let $S=\\{(F_{1},0,\dots,0),(0,F_{2},0,\dots,0),\ldots,(0,\dots,0,F_{n})\\}$.
If $I,J\in S$, then $I+J\neq R$, $I\nsubseteq J$ and $J\nsubseteq I$. Thus $I$
is adjacent to $J$ in $\Gamma(R)^{**}[A]$. Therefore, the induced subgraph on
$S$ is a clique in $\Gamma(R)^{**}[A]$. Hence suppose that $I\in
V(\Gamma(R)^{**})\setminus S\cup\mathrm{Max}(R)$. Then $I$ is adjacent to some
vertices of $S$ in $\Gamma(R)^{**}[A]$, because at least two components of $I$
are zero.

$(3)$ By $(2)$, it is obvious.

$(4)$ First we claim that $V(\Gamma(R)^{*})=V(\Gamma(R)_{SR})$. By parts (2)
and (3), $V(\Gamma(R)^{*})=V({\Gamma}(R))\setminus\mathrm{Max}(R)$. We prove
that $V(\Gamma(R)_{SR})=V({\Gamma}(R))\setminus\mathrm{Max}(R)$. Let
$I\in\mathrm{Max}(R)$. It is shown that there is no $J\in V(\Gamma(R))$ such
that $I$ and $J$ are mutually maximally distant. If not, $I\nsubseteq J$ and
$J\nsubseteq I$, by Lemma 2.4. Since $I\in\mathrm{Max}(R)$, $I+J=R$ in
$\Gamma(R)$. Hence $d(I,J)=1$. If $J\neq Ann(I)$, then $d(I,Ann(I))=1$, but
$Ann(I)\subseteq J$ and so $d(J,Ann(I))\neq 1$, a contradiction. If
$J=Ann(I)$, for some $Ann(I)\subseteq K$, then $d(I,Ann(I))=1$, but
$Ann(I)\subseteq K$ and so $d(K,Ann(I))\neq 1$, a contradiction. Hence $I$ is
an isolated vertex in $\Gamma(R)_{SR}$ and so
$V(\Gamma(R)_{SR})\cap\mathrm{Max}(R)=\varnothing$. Next, assume that
$S=\\{I\in V({\Gamma}(R))\,|\,NZC(I)=1\\}$ and $I,J\in S$. Then
$d(I,J)_{{\Gamma}(R)}=3=diam({\Gamma}(R))$. This implies that $I,J$ are
mutually maximally distant and hence $I$ is adjacent to $J$ in
${\Gamma}(R)_{SR}$. Therefore, the induced subgraph on $S$ is a clique in
${\Gamma}(R)_{SR}$. Now, we show that if $I\not\in\mathrm{Max}(R)\cup S$, then
$I$ is adjacent to some vertices of $S$ in ${\Gamma}(R)_{SR}$. But this is
obvious, because it is not hard to see that $d(I,J)=3=daim(\Gamma(R))$, for
some $J\in S$. Therefore,
$\partial({\Gamma}(R))=V({\Gamma}(R))\setminus\mathrm{Max}(R)$ and so the
claim is proved. To complete the proof, it is enough to show that the
adjacency between vertices in $\Gamma(R)^{*}$ is in a one to one
correspondence between vertices in $\Gamma(R)_{SR}$ and vice versa. Assume
that $I,J\in V(\Gamma(R)^{*})$ and $I$ is adjacent to $J$. Thus $I+J\neq R$,
$I\nsubseteq J$ and $J\nsubseteq I$. Since $I+J\neq R$,
$d(I,J)_{\Gamma(R)}\neq 1$. Indeed, $d(I,J)_{\Gamma(R)}\in\\{2,3\\}$. If
$d(I,J)_{\Gamma(R)}=3$, then $d(I,J)=daim(\Gamma(R))$. So $I,J$ are mutually
maximally distant. Hence $I$ is adjacent to $J$ in $\Gamma(R)_{SR}$.
Therefore, suppose that $d(I,J)_{\Gamma(R)}=2$ and $K\in N_{\Gamma(R)}(I)$.
Since $I+K=R$ and $J\nsubseteq I$, $K\cap J\neq 0$. By Lemma 2.4,
$d(K,J)_{\Gamma(R)}\leq 2$. Thus $d(K,J)_{\Gamma(R)}\leq d(I,J)_{\Gamma(R)}$.
Similarly, $d(L,I)_{\Gamma(R)}\leq d(I,J)_{\Gamma(R)}=2$, for every $L\in
N(J)$. So $I,J$ are mutually maximally distant and hence $I$ is adjacent to
$J$ in $\Gamma(R)_{SR}$. Finally,l let $I,J\in V(\Gamma(R)_{SR})$ and $I$ is
adjacent to $J$. Thus $I,J$ are mutually maximally distant. By Lemma 2.4,
$I+J\neq R$, $I\nsubseteq J$ and $J\nsubseteq I$ and so $I$ is adjacent to $J$
in $\Gamma(R)^{*}$.  $\Box$

###### Lemma 2.6

Suppose that $R\cong F_{1}\times\cdots\times F_{n}$, where $F_{i}$ is a field
for every $1\leq i\leq n$ and $S$ is the largest independent set of
$\Gamma(R)_{SR}$. Then the followings hold.

$(1)$ $\partial({\Gamma}(R))=V({\Gamma}(R))\setminus\mathrm{Max}(R)$.

$(2)$ $NZC(I)=n-2$, for some $I\in S$.

$(3)$ $\beta({\Gamma}(R)_{SR})=n-2$, if $n\geq 3$.

###### Proof.

$(1)$ By Lemma 2.5, it is obvious.

$(2)$ Assume that $S=\\{I_{1},I_{2},\dots,I_{t}\\}$ is the largest independent
set of $\Gamma(R)_{SR}$. Clearly, $1\leq NZC(I)\leq n-2$, for every $I\in
V({\Gamma}(R)_{SR})$. With no loss of generality, assume that $NZC(I_{t})\geq
NZC(I_{i})$ for every $1\leq i\leq t$ and $I_{i}\in S$. We claim that
$NZC(I_{t})=n-2$. Assume to the contrary, $NZC(I_{t})\leq n-3$. Since $I_{t}$
is not adjacent to $I_{i}$, for every $1\leq i\leq t$ and $I_{i}\in S$, we
deduce that $I_{i}\subseteq I_{t}$ or $I_{t}\subseteq I_{i}$ or
$I_{t}+I_{i}=R$. If $I_{t}\subseteq I_{i}$, for some $I_{i}\in S$, then
$NZC(I_{i})\geq NZC(I_{t})$, a contradiction. This implies that
$I_{i}\subseteq I_{t}$ or $I_{t}+I_{i}=R$, for every $I_{i}\in S$ and $1\leq
i\leq t$. Assume that $S_{1}=\\{I\in S\,\,|\,\,I\subseteq I_{t}\\}$ and
$S_{2}=\\{I\in S\,\,|\,\,I+I_{t}=R\\}$. Since $NZC(I_{t})\leq n-3$, by
replacing one of the zero components of $I_{t}$ by $F_{i}$, we get $J\in
V({\Gamma}(R)_{SR})$. Hence $I\subseteq J$, for every $I\in S_{1}$ and
$I+J=R$, for every $I\in S_{2}$. This implies that $S\cup\\{J\\}$ is a
independent set of $\Gamma(R)_{SR}$, a contradiction. Therefore,
$NZC(I_{t})=n-2$ which completes the proof.

$(3)$ Assume that

$A_{1}=\\{I\in V({\Gamma}(R)_{SR})\,|\,NZC(I)=1\\}$,

$A_{2}=\\{I\in V({\Gamma}(R)_{SR})\,|\,NZC(I)=2\\}$,

$A_{3}=\\{I\in V({\Gamma}(R)_{SR})\,|\,NZC(I)=3\\}$,

$\vdots$

$A_{n-2}=\\{I\in V({\Gamma}(R)_{SR})\,|\,NZC(I)=n-2\\}$. Consider the
following facts:

Fact 1. For every $1\leq i\leq n-2$ and $I,J\in A_{i}$, since $NZC(I)=NZC(J)$,
we conclude that $I\nsubseteq J$ and $J\nsubseteq I$.

Fact 2. If $I,J\in A_{i}$ for every $1\leq i\leq n-2$ and $I$ is not adjacent
to $J$, then $I+J=R$, by Fact 1.

Fact 3. Since there is no $I,J\in A_{i}$ such that $I+J=R$, for every $1\leq
i\leq[\dfrac{n-2}{2}]-1$, we conclude that ${\Gamma}(R)_{SR}[A_{i}]$ is
complete, by Fact 2.

Fact 4. For every $[\dfrac{n-2}{2}]\leq i\leq n-2$, assume that
$S_{i}\subseteq A_{i}$ is the largest set of $A_{i}$ such that for every
$I,J\in S_{i}$, $I+J=R$. Then $|S_{i}|=[\dfrac{n}{n-i}]$.

Continue the proof in the following steps:

Step 1. Put
$I_{1}=(F_{1},0,\dots,0),I_{2}=(F_{1},F_{2},0,\dots,0),\dots,I_{i}=(F_{1},F_{2},\dots,F_{i},\dots,0)$,
where $i=[\dfrac{n-2}{2}]-1$ and $W_{1}=\\{I_{1},I_{2},\dots,I_{i}\\}$. Then
$W_{1}$ is an independent set of ${\Gamma}(R)_{SR}[\cup A_{i}]$. Since by Fact
3 ${\Gamma}(R)_{SR}[A_{i}]$ is complete and $|W_{1}\cap A_{i}|=1$, we deduce
that $W_{1}$ is the largest independent set of ${\Gamma}(R)_{SR}[\cup A_{i}]$,
for $i=1$ up to $i=[\dfrac{n-2}{2}]-1$ (if $n$ is odd up to
$[\dfrac{n-2}{2}])$. This implies that $\beta({\Gamma}(R)_{SR}[\cup
A_{i}])=|W_{1}|=[\dfrac{n-2}{2}]-1$.

Step 2. Consider $A_{i}$, where $i=[\dfrac{n-2}{2}]$ and $n$ is even. Then we
have $|S_{i}|=2$. Without loss of generality, assume that
$S_{i}=\\{I=(F_{1},F_{2},\dots,F_{i},\dots,0),J=(0,\dots,0,F_{i+1},\dots,F_{n})\\}$.
This implies that $J$ is adjacent to some vertices of $W_{1}$, but
$W_{2}=W_{1}\cup\\{I\\}$ is the largest independent set of
${\Gamma}(R)_{SR}[\cup A_{i}]$ for $i=1$ up to $i=[\dfrac{n-2}{2}]$. Hence
$\beta({\Gamma}(R)_{SR}[\cup A_{i}])=|W_{2}|=[\dfrac{n-2}{2}]$.

Step 3. Continue the procedure in Step 2 up to $t=n-2-[\dfrac{n-2}{2}]$ and
get
$W_{t}=\\{(F_{1},0,\dots,0),(F_{1},F_{2},0,\dots,0),\dots,(F_{1},F_{2},\dots,F_{n-2},0,0)\\}$.
Therefore,

$\beta({\Gamma}(R)_{SR}[\cup_{1}^{n-2}A_{i}])=\beta({\Gamma}(R)_{SR})=|W_{t}|=n-2.$

$\Box$

We are now in a position to state the main result of this section.

###### Theorem 2.1

Suppose that $R$ is a reduced ring. If $sdim_{M}({\Gamma}(R))$ is finite, then

$(1)$ If $|\mathrm{Max}(R)|=2$, then $sdim_{M}({\Gamma}(R))=1$.

$(2)$ If $|\mathrm{Max}(R)|=n\geq 3$, then $sdim_{M}({\Gamma}(R))=2^{n}-2n$.

###### Proof.

Since $sdim_{M}({\Gamma}(R))$ is finite, $R$ has finitely many ideals, by
Corollary 2.1, and so [4, Theorem 8.7] implies that $R\cong
F_{1}\times\cdots\times F_{n}$, where $F_{i}$ is a field for every $1\leq
i\leq n=|\mathrm{Max}(R)|$. If $n=2$, then $\Gamma(R)=K_{2}$ and so
$sdim_{M}({\Gamma}(R))=1$. If $n\geq 3$, then by Lemmas 2.2 and 2.3,

$sdim_{M}({\Gamma}(R))=\alpha({\Gamma}(R)_{SR})=V({\Gamma}(R)_{SR})-\beta({\Gamma}(R)_{SR}).$

On the other hand, $\partial({\Gamma}(R))=2^{n}-2-n$ and
$\beta({\Gamma}(R)_{SR})=n-2$, by Lemma 2.6. Therefore,
$sdim_{M}({\Gamma}(R))=2^{n}-2-n-(n-2)=2^{n}-2n$.  $\Box$

##  3 Non-reduced rings case

In this section, we study the strong metric dimension of $\Gamma(R)$, when $R$
is non-reduced.

As we have seen in Corollary 2.1, $sdim_{M}(\Gamma(R))$ is finite if and only
if $\Gamma(R)$ is finite. Hence in this section, we focus on rings with
finitely many ideals. Obviously, such rings are Artinian and it follows from
[4, Theorem 8.7] that there exists a positive integer $n$ such that $R\cong
R_{1}\times\cdots\times R_{n}$, where $R_{i}$ is an Artinian local ring for
every $1\leq i\leq n$. If every $R_{i}$ is non-reduced, then it is not a field
and so $|A(R_{i})^{*}|\geq 1$ (It is not hard to see that if $R$ is Artinian
and non-reduced, then every proper ideal has a non-zero annihilator). Now, let
$R$ be a such ring. Suppose that $I=(I_{1},\dots,I_{n})$ and
$J=(J_{1},\dots,J_{n})$ are two vertices of $\Gamma(R)$. Define the relation
$\thicksim$ on $A(R)^{*}$ as follows: $I\thicksim J$, whenever for each $1\leq
i\leq n$, “$I_{i}\subseteq Nil(R_{i})$ if and only if $J_{i}\subseteq
Nil(R_{i})$”. Clearly, $\thicksim$ is an equivalence relation on $A(R)^{*}$.
The equivalence class of $I$ is denoted by $[I]$. Suppose that
$I=(I_{1},\dots,I_{n})$ is and ideal of $R$. Then by
$I^{\prime}=(I_{1}^{\prime},\dots,I_{n}^{\prime})$, we mean a new ideal
obtained from $I$ whose all nonzero nilpotent components are replaced by $0$.

We define $\Gamma(R)^{\prime}$ as the graph with vertex set
$V(\Gamma(R)^{\prime})=V(\Gamma(R))$ such that two vertices $I,J$ are adjacent
in $\Gamma(R)^{\prime}$ if and only if $[I]=[J]$ or $I^{\prime}+J^{\prime}\neq
R$, $I^{\prime}\nsubseteq J^{\prime}$ and $J^{\prime}\nsubseteq I^{\prime}$.

The proof of the following lemma is obvious.

###### Lemma 3.1

Suppose that $R\cong R_{1}\times\cdots\times R_{n}$, where $R_{i}$ is an
Artinian local ring for every $1\leq i\leq n$. If $I$ and $J$ are vertices of
$\Gamma(R)$ with $[I]=[J]$, then $N[I]=N[J]$.

###### Lemma 3.2

Suppose that $R\cong R_{1}\times\cdots\times R_{n}$, where $R_{i}$ is an
Artinian local ring and $|A(R_{i})^{*}|\geq 1$, for every $1\leq i\leq n$.
Then the following statements hold.

$(1)$
$\Gamma(R)^{\prime}=H^{\prime}+K_{|A(R_{1})|}+K_{|A(R_{2})|}+\cdots+K_{|A(R_{n})|}$,
where $H^{\prime}=\varnothing$ if $n=2$ and $H^{\prime}$ is connected if
$n\geq 3$.

$(2)$ $\Gamma(R)^{\prime}=\Gamma(R)_{SR}$.

###### Proof.

Let

$A_{1}=\\{I\in V({\Gamma}(R))\,|\,[I]=[(Nil(R_{1}),R_{2},\dots,R_{n})]\\}$,

$A_{2}=\\{I\in
V({\Gamma}(R))\,|\,[I]=[(R_{1},Nil(R_{2}),R_{3},\dots,R_{n})]\\}$,

$\vdots$

$A_{n}=\\{I\in V({\Gamma}(R))\,|\,[I]=[(R_{1},\dots,R_{n-1},Nil(R_{n}))]\\}$
and

$A=\cup A_{i}$.

Obviously, if $I,J\in A_{i}$, then $[I]=[J]$ and so $I$ is adjacent to $J$ in
$\Gamma(R)^{\prime}$. This implies that $\Gamma(R)^{\prime}[A_{i}]$ is a
complete graph, for every $1\leq i\leq n$. Suppose that $I\in A_{i}$ and
$J\not\in A_{i}$. We show that $I$ is not adjacent to $J$ in
$\Gamma(R)^{\prime}$. If not, since $[I]\neq[J]$, we must have
$I^{\prime}+J^{\prime}\neq R$, $I^{\prime}\nsubseteq J^{\prime}$ and
$J^{\prime}\nsubseteq I^{\prime}$, a contradiction, as
$I^{\prime}+J^{\prime}\neq R$.

Next, it is proved that $\Gamma(R)^{\prime}\setminus A$ is a connected graph.
For this, let

$S=\\{(R_{1},0,\dots,0),(0,R_{2},0,\dots,0),\ldots,(0,\dots,0,R_{n})\\}.$

Using a proof technique similar to Lemma 2.5 implies that the induced subgraph
on $S$ is a clique in $\Gamma(R)^{\prime}$ and every $I\in
V(\Gamma(R)^{\prime})\setminus(S\cup A)$ is adjacent to some vertices of $S$
in $\Gamma(R)^{\prime}$. Therefore,
$\Gamma(R)^{\prime}=H^{\prime}+K_{|A(R_{1})|}+K_{|A(R_{2})|}+\cdots+K_{|A(R_{n})|}$.
By an easy verification, if $n=2$, then $H^{\prime}=\varnothing$.

$(2)$ Let $I\in V(\Gamma(R)^{\prime})$. Since $|[I]|\geq 2$, $I\sim J$ and
$I\neq J$. Since $N(I)=N(J)$, we deduce that $I$ and $J$ are mutually
maximally distant and hence $I\in V(\Gamma(R)_{SR})$. Thus
$V(\Gamma(R)^{\prime})=V(\Gamma(R)_{SR})$.

We show that $\Gamma(R)^{\prime}=\Gamma(R)_{SR}$. Let $I,J\in
V(\Gamma(R)^{\prime})$ and $I$ is adjacent to $J$. If $[I]=[J]$, then $I,J$
are mutually maximally distant and so $I$ is adjacent to $J$ in
$\Gamma(R)_{SR}$. If $[I]\neq[J]$, then $I^{\prime}+J^{\prime}\neq R$,
$I^{\prime}\nsubseteq J^{\prime}$ and $J^{\prime}\nsubseteq I^{\prime}$. By a
similar proof to that of Lemma 2.5, $I^{\prime},J^{\prime}$ are mutually
maximally distant. Since $[I]=[I^{\prime}]$ and $[J]=[J^{\prime}]$, $I$ is
adjacent to $J$ in $\Gamma(R)_{SR}$.

Finally, suppose that $I,J\in V(\Gamma(R)_{SR})$ and $I$ is adjacent to $J$.
If $[I]=[J]$, then $N(I)=N(J)$ and so $I,J$ are mutually maximally distant. If
$[I]\neq[J]$, then $I+J\neq R$, $I\nsubseteq J$ and $J\nsubseteq I$. Thus
$I^{\prime}+J^{\prime}\neq R$, $I^{\prime}\nsubseteq J^{\prime}$ and
$J^{\prime}\nsubseteq I^{\prime}$. Hence $I$ is adjacent to $J$ in
$V(\Gamma(R)^{\prime})$.  $\Box$

###### Lemma 3.3

Suppose that $R\cong R_{1}\times\cdots\times R_{n}$, where $R_{i}$ is an
Artinian local ring and $|A(R_{i})^{*}|\geq 1$, for every $1\leq i\leq n$.
Then the following statements hold.

$(1)$ $\partial({\Gamma}(R))=V({\Gamma}(R))$.

$(2)$ $\beta({\Gamma}(R)_{SR})=2n-2$.

###### Proof.

$(1)$ is obvious.

$(2)$ By the proof of Lemma 3.2,
$\Gamma(R)_{SR}=H^{\prime}+K_{|A(R_{1})|}+K_{|A(R_{2})|}+\cdots+K_{|A(R_{n})|}$.
It is known that
$\beta(K_{|A(R_{1})|}+K_{|A(R_{2})|}+\cdots+K_{|A(R_{n})|})=n$. Let

$A=\\{(I_{1},\dots,I_{n})\in
V(H^{\prime})\,\,|\,\,I_{i}\in\\{0,R_{1},\dots,R_{n}\\}\,\,\mathrm{for\,\,every}\,\,1\leq
i\leq n\\}.$

Then $H^{\prime}[A]\cong H$, where $H=\Gamma(R)^{**}-n(K_{1})$ (as in part (3)
of Lemma 2.5). Thus $|A\cap[I]|=1$, for every equivalence class $[I]$. On the
other hand, since $H^{\prime}[[I]]$ is complete for every $I\in H^{\prime}$,
$\beta(H^{\prime})=\beta(\Gamma(R)_{SR}[A])$. Since $\Gamma(R)_{SR}[A]\cong H$
and $\beta(H)=n-2$, we deduce that
$\beta(H^{\prime})=\beta(\Gamma(R)_{SR}[A])=n-2$. Therefore,
$\beta({\Gamma}(R)_{SR})=\beta(H^{\prime})+n=2n-2$.  $\Box$

Now, we are ready to state the following result.

###### Theorem 3.1

Suppose that $R\cong R_{1}\times\cdots\times R_{n}$, where $R_{i}$ is an
Artinian local ring and $|A(R_{i})^{*}|\geq 1$, for every $1\leq i\leq n$.
Then $sdim_{M}(\Gamma(R))=|V(\Gamma(R))|-2n+2$.

###### Proof.

The proof follows from Lemmas 2.2, 2.3 and 3.3. $\Box$

The following example is related to Theorem 3.1.

###### Example 3.1

Let $R=\mathbb{Z}_{4}\times\mathbb{Z}_{4}\times\mathbb{Z}_{8}$. Then

$[(\mathbb{Z}_{4},\mathbb{Z}_{4},(0))]=\\{(\mathbb{Z}_{4},\mathbb{Z}_{4},(0)),(\mathbb{Z}_{4},\mathbb{Z}_{4},(2)),(\mathbb{Z}_{4},\mathbb{Z}_{4},(4))\\}$,

$[(\mathbb{Z}_{4},(0),\mathbb{Z}_{8})]=\\{(\mathbb{Z}_{4},(0),\mathbb{Z}_{8}),(\mathbb{Z}_{4},(2),\mathbb{Z}_{8})\\}$,

$[((0),\mathbb{Z}_{4},\mathbb{Z}_{8})]=\\{((0),\mathbb{Z}_{4},\mathbb{Z}_{8}),((2),\mathbb{Z}_{4},\mathbb{Z}_{8})\\}$,

$[((0),(0),\mathbb{Z}_{8})]=\\{((0),(0),\mathbb{Z}_{8}),((2),(0),\mathbb{Z}_{8}),((0),(2),\mathbb{Z}_{8}),((2),(2),\mathbb{Z}_{8})\\}$,

$[(\mathbb{Z}_{4},(0),(0))]=\\{(\mathbb{Z}_{4},(0),(0)),(\mathbb{Z}_{4},(0),(2)),(\mathbb{Z}_{4},(0),(4)),(\mathbb{Z}_{4},(2),(0)),(\mathbb{Z}_{4},(2),(2)),(\mathbb{Z}_{4},(2),(4))\\}$,

$[((0),\mathbb{Z}_{4},(0))]=\\{((0),\mathbb{Z}_{4},(0)),((0),\mathbb{Z}_{4},(2)),((0),\mathbb{Z}_{4},(4)),((2),\mathbb{Z}_{4},(0)),((2),\mathbb{Z}_{4},(2)),((2),\mathbb{Z}_{4},(4))\\}$
and

$A=\\{(\mathbb{Z}_{4},\mathbb{Z}_{4},(0)),(\mathbb{Z}_{4},(0),\mathbb{Z}_{8}),((0),\mathbb{Z}_{4},\mathbb{Z}_{8}),((0),(0),\mathbb{Z}_{8}),(\mathbb{Z}_{4},(0),(0)),((0),\mathbb{Z}_{4},(0))\\}$.

Let $J,K\in[I]$. Then $N[J]=N[K]$ and so by Lemma 2.1, $J\in W$ or $K\in W$,
where $W$ is a strong resolving set of $\Gamma(R)$. Thus one may assume that
$V(\Gamma(R))\setminus A\subseteq W$. Let

$V_{1}=\\{((0),\mathbb{Z}_{4},(0)),(\mathbb{Z}_{4},(0),(0)),((0),(0),\mathbb{Z}_{8})\\}$
and

$V_{2}=\\{(\mathbb{Z}_{4},(0),\mathbb{Z}_{8}),((0),\mathbb{Z}_{4},\mathbb{Z}_{8}),(\mathbb{Z}_{4},\mathbb{Z}_{4},(0))\\}$.

Since $d(I,J)=daim(\Gamma(R))$, for every $I,J\in V_{1}$, we conclude that
$I\in W$ or $J\in W$. Thus one may let $V(\Gamma(R))\setminus V_{3}\subseteq
W$, where $V_{3}=V_{2}\cup\\{((0),(0),\mathbb{Z}_{8})\\}$.

Since for every $I,J\in V_{3}$ we have $N[I]\neq N[J]$, $I,J$ are strongly
resolved by some vertices of $W=A(R)^{*}\setminus(V_{3})$. This means that
$sdim_{M}(\Gamma)=|W|=23-4=19$. On the other hand, by Theorem 3.1 it is easily
seen that $sdim_{M}({\Gamma}(R))=23-6+2=19$.

We close this paper with the following result which completely characterizes
$sdim_{M}({\Gamma}(R))$, when $R$ is non-reduced.

###### Corollary 3.1

Let $R\cong R_{1}\times\cdots\times R_{n}\times F_{1}\times\cdots\times
F_{m}$, be a ring, $n\geq 1$, $m\geq 1$ where $R_{i}$ is an Artinian local
ring such that for every $1\leq i\leq n$, $|A(R_{i})^{*}|\geq 1$ and each
$F_{i}$ is a field. Then $sdim_{M}(\Gamma(R))=|V(\Gamma(R))|-2n-2m+2$.

###### Proof.

The proof here is a refinement of the arguments in proofs of Theorem 3.1 and
Lemma 2.5. By similar proofs, one may get
$\Gamma(R)_{SR}=H^{\prime\prime}+K_{|A(R_{1})|}+K_{|A(R_{2})|}+\cdots+K_{|A(R_{n})|}$,
$\beta(H^{\prime\prime})=n+m-2$ and
$\beta(K_{|A(R_{1})|}+K_{|A(R_{2})|}+\cdots+K_{|A(R_{n})|})=n$. Thus
$\beta({\Gamma}(R)_{SR})=2n+m-2$. On the other hand, since
$|V(\Gamma(R)_{SR})|=|V(\Gamma(R))|-m$, we deduce that
$sdim_{M}(\Gamma(R))=|V(\Gamma(R))|-m-(2n+m-2)=|V(\Gamma(R))|-2n-2m+2$.
$\Box$

Acknowledgements. Reza Nikandish in this work has been financially supported
by the research of Jundi-Shapur Research Institute. The grant number was
01-100-1-1400.

## References

  * [1] M. Afkhami, K. Khashyarmanesh, The comaximal graph of a lattice, Bull Malays. Sci. Soc. 37 (2) (2014) 261–269.
  * [2] S. Akbari, B. Miraftab, R. Nikandish, A note on co-maximal ideal graph of commutative rings, Ars. Combin. 134 (2017) 261–265.
  * [3] S. Akbari, B. Miraftab, R. Nikandish, Co-maximal graphs of subgrous of groups, Canad. Math. Bull. 60 (1) (2017) 12–25.
  * [4] M. F. Atiyah, I. G. Macdonald, Introduction to Commutative Algebra, Addison-Wesley Publishing Company (1969).
  * [5] R. F. Bailey, P. J. Cameron, Base size, metric dimension and other invariants of groups and graphs, Bull. London Math. Soc. 43 (2011) 209–242.
  * [6] P. S. Buczkowski, G. Chartrand, C. Poisson, P. Zhang, On k-dimensional graphs and their bases, Period. Math. Hung. 46 (2003) 9–15.
  * [7] G. Chartrand, L. Eroh, M. A. Johnson, O. R. Oellermann, Resolvability in graphs and the metric dimension of a graph, Disc. Appl. Math. 105 (2000) 99–113.
  * [8] D. Dol${\rm\check{z}}$an, The metric dimension of the total graph of a finite commutative ring, Canad. Math. Bull. 59 (2016) 748-759.
  * [9] D. Dol${\rm\check{z}}$an, The metric dimension of the annihilating-ideal graph of a finite commutative ring, Bull. Aust. Math. Soc. 103 (2021) 362–368.
  * [10] Sh. Ebrahimi, R. Nikandish, A. Tehranian, H. Rasouli, On the strong metric dimension of annihilator graphs of commutative rings, Bull. Malays. Math. Sci. Soc. 44 (2021) 2507–2517.
  * [11] F. Harary, R. A. Melter, On the metric domension of a graph. Ars Combin. 2 (1976) 191–195.
  * [12] M. Imran, A. Q. Baig, S. A. U. H. Bokhary, and I. Javaid, On the metric dimension of circulant graphs, Appl. Math. Lett. 25 (2012) 320–325.
  * [13] M. Imran Bhat, S. Pirzada, On strong metric dimension of zero-divisor graphs of rings, Korean J. Math. 27 (2019) 563–580.
  * [14] Z. Jiang, N. Polyanskii, On the metric dimension of Cartesian powers of a graph, J. Comb. theory Ser. A. 165 (2019) 1–14.
  * [15] S. Khuller, B. Raghavachari, A. Rosenfeld, Localization in graphs, Technical report CS-TR-3326, University of Maryland at College Park, (1994).
  * [16] S. Klav${\rm\check{z}}$ar, F. Rahbarnia, M. Tavakolid, Some binary products and integer linear programming for $k$-metric dimension of graphs, Appl. Math. Comput. 409 (15) (2021) 126420.
  * [17] D. Kuziak, I. G. Yero, J. A. Rodrguez-Velzquez, On the strong metric dimension of corona product graphs and join graphs, Discrete Appl. Math. (2013) 161 1022–1027.
  * [18] D. Kuziak, I. G. Yero, J. A. Rodrguez-Velquez, On the strong metric dimension of the strong products of graphs, Open Math. (2015) 13 64–74.
  * [19] X. Ma, M. Feng, K. Wang, The strong metric dimension of the power graph of a finite group, Discrete Appl. Math. 239 (2018) 159–164.
  * [20] X. Ma, L. Zhai, Strong metric dimensions for power graphs of finite groups, Comm. Algebra (2021) DOI: 10.1080/00927872.2021.1924764.
  * [21] B. Miraftab, R. Nikandish, Co-maximal ideal graphs of matrix algebras, Bol. Soc. Mat. Mex. 24 (2018) 1–10.
  * [22] B. Miraftab, R. Nikandish, Co-maximal graphs of two generate groups, J. Algebra. Appl. 18 (4) (2019) (13 pages).
  * [23] R. Nikandish, M. J. Nikmehr, M. Bakhtyiari, Metric and strong metric dimension in cozero-divisor graphs, Mediterr. J. Math. 18 112 (2021).
  * [24] O. R. Oellermann, J. Peters-Fransen, The strong metric dimension of graphs and digraphs, Discrete Appl. Math. 155 (2007) 356–364.
  * [25] S. Pirzada, R. Raja , On the metric domension of a zero-divisor graph, Comm. Algebra 45 (4) (2017) 1399–1408.
  * [26] A. Sebö, E. Tannier, On metric generators of graphs, Math. Oper. Res. 29 (2004) 383–393.
  * [27] S. Shen, W. Liu, L. Feng, Some properties of comaximal right ideal graph of a ring, Appl. Math. Comput. 333 (15) (2018) 225–230.
  * [28] D. B. West, Introduction to Graph Theory, 2nd ed., Prentice Hall, Upper Saddle River (2001).
  * [29] T. S. Wu, M. Ye, Q, Liu, J. Guo, Graph properties of co-maximal ideal graphs of commutative rings, J. Algebra. Appl. 14 (3) (2015) (13 pages).
  * [30] M. Ye, T. S. Wu, Co-maximal ideal graphs of commutative rings, J. Algebra. Appl. 16(6) (2012) (14 pages).
  * [31] M. Ye, T. S. Wu, Q. Liu, H. Yu, Implements of graphs blow-up in co-maximal ideal graphs, Comm. Algebra 42 (2014) 2476–2483.


# Quantum++
A C++11 quantum computing library

Author: Vlad Gheorghiu
Institute for Quantum Computing, University of Waterloo,
Waterloo, ON, N2L 3G1, Canada
<EMAIL_ADDRESS>
(Version of: October 10, 2016)

Abstract:  Quantum++ is a modern general-purpose multi-threaded quantum
computing library written in C++11 and composed solely of header files. The
library is not restricted to qubit systems or specific quantum information
processing tasks, being capable of simulating arbitrary quantum processes. The
main design factors taken in consideration were the ease of use, portability,
and performance.

###### Contents

  1. 1 Introduction
  2. 2 Installation
  3. 3 Data types, constants and global objects
    1. 3.1 Data types
    2. 3.2 Constants
    3. 3.3 Singleton classes and their global instances
  4. 4 Simple examples
    1. 4.1 Gates and states
    2. 4.2 Measurements
    3. 4.3 Quantum operations
    4. 4.4 Timing
    5. 4.5 Input/output
    6. 4.6 Exceptions
  5. 5 Brief description of Quantum++ file structure
  6. 6 Advanced topics
    1. 6.1 Aliasing
    2. 6.2 Type deduction via auto
    3. 6.3 Optimizations
    4. 6.4 Extending Quantum++

###### Listings

  1. 1 Minimal example
  2. Listing 1 output
  3. 2 Gates and states
  4. Listing 2 output
  5. 3 Measurements
  6. Listing 3 output
  7. 4 Quantum operations
  8. Listing 4 output
  9. 5 Timing
  10. Listing 5 output
  11. 6 Input/output
  12. Listing 6 output
  13. 7 Exceptions
  14. Listing 7 output

##  1 Introduction

Quantum++, available online at http://vsoftco.github.io/qpp, is a C++11
general purpose quantum computing library, composed solely of header files. It
uses the Eigen 3 linear algebra library and, if available, the OpenMP multi-
processing library. For additional Eigen 3 documentation see
http://eigen.tuxfamily.org/dox/. For a simple Eigen 3 quick ASCII reference
see http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt.

The simulator defines a large collection of (template) quantum computing
related functions and a few useful classes. The main data types are complex
vectors and complex matrices, which I will describe below. Most functions
operate on such vectors/matrices and _always_ return the result by value.
Collection of objects are implemented via the standard library container
`std::vector<>`, instantiated accordingly.

Although there are many available quantum computing libraries/simulators
written in various programming languages, see [1] for a comprehensive list, I
hope what makes Quantum++ different is the ease of use, portability and high
performance. The library is not restricted to specific quantum information
tasks, but it is intended to be multi-purpose and capable of simulating
arbitrary quantum processes. I have chosen the C++ programming language
(standard C++11) in implementing the library as it is by now a mature
standard, fully (or almost fully) implemented by most important compilers, and
highly portable.

In the reminder of this manuscript I describe the main features of the
library, “in a nutshell” fashion, via a series of simple examples. I assume
that the reader is familiar with the basic concepts of quantum
mechanics/quantum information, as I do not provide any introduction to this
field. For a comprehensive introduction to the latter see e.g. [2]. This
document is not intended to be a comprehensive documentation, but only a brief
introduction to the library and its main features. For a detailed reference
see the official manual available as a `.pdf` file in `./doc/refman.pdf`. For
detailed installation instructions as well as for additional information
regarding the library see the main repository page at
http://vsoftco.github.io/qpp. If you are interesting in contributing, or for
any comments or suggestions, please email me at<EMAIL_ADDRESS>
Quantum++ is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

Quantum++ is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
Quantum++. If not, see http://www.gnu.org/licenses/.

##  2 Installation

To get started with Quantum++, first install the Eigen 3 library from
http://eigen.tuxfamily.org into your home directory111I implicitly assume that
you use a UNIX-like system, although everything should translate into Windows
as well, with slight modifications, as `$HOME/eigen`. You can change the name
of the directory, but in the current document I will use `$HOME/eigen` as the
location of the Eigen 3 library. Next, download the Quantum++ library from
http://vsoftco.github.io/qpp/ and unzip it into the home directory as
`$HOME/qpp`. Finally, make sure that your compiler supports C++11 and
preferably OpenMP. For a compiler I recommend g++ version 4.8.2 or later or
clang version 3.7 or later (previous versions of clang do not support OpenMP).
You are now ready to go!

We next build a simple minimal example to test that the installation was
successful. Create a directory called `$HOME/testing`, and inside it create
the file `minimal.cpp`, with the content listed in Listing 1. A verbatim copy
of the above program is also available at `$HOME/qpp/examples/minimal.cpp`.

⬇

1// Minimal example

2// Source: ./examples/minimal.cpp

3#include <iostream>

4#include "qpp.h"

5

6using namespace qpp;

7

8int main()

9{

10 std::cout << "Hello Quantum++!\nThis is the |0> state:\n";

11 std::cout << disp(st.z0);

12}

Listing 1: Minimal example

Next, compile the file using a C++11 compliant compiler. In the following I
assume you use g++, but the building instructions are similar for other
compilers. From the directory `$HOME/testing` type

    g++ -std=c++11 -isystem $HOME/eigen -I $HOME/qpp/include minimal.cpp -o minimal

Your compile command may differ from the above, depending on the C++ compiler
and operating system. If everything went fine, the above command should build
an executable `minimal` in `$HOME/testing`, which can be run by typing
`./minimal`. The output should be similar to the following:

⬇

Hello Quantum++!

This is the |0> state:

1.0000

0

Listing 1 output

In line 4 of Listing 1 we include the main header file of the library `qpp.h`
This header file includes all other necessary internal Quantum++ header files.
In line 11 we display the state $|0\rangle$ represented by the singleton
`st.z0` in a nice format using the display manipulator `disp()`.

##  3 Data types, constants and global objects

All header files of Quantum++ are located inside the `./include` directory.
All functions, classes and global objects defined by the library belong to the
`namespace qpp`. To avoid additional typing, I will omit the prefix `qpp::` in
the rest of this document. I recommend to use using namespace qpp; in your
main `.cpp` file.

###  3.1 Data types

The most important data types are defined in the header file `types.h`. We
list them in Table 1.

`idx` |  Index (non-negative integer), alias for `std::size_t`
---|---
`bigint` |  Big integer, alias for `long long int`
`cplx` |  Complex number, alias for `std::complex<double>`
`cmat` |  Complex dynamic matrix, alias for `Eigen::MatrixXcd`
`dmat` |  Double dynamic matrix, alias for `Eigen::MatrixXd`
`ket` |  Complex dynamic column vector, alias for `Eigen::VectorXcd`
`bra` |  Complex dynamic row vector, alias for `Eigen::RowVectorXcd`
`dyn_mat<Scalar>` |  Dynamic matrix template alias over the field `Scalar`, alias for `Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>`
`dyn_col_vect<Scalar>` |  Dynamic column vector template alias over the field `Scalar`, alias for `Eigen::Matrix<Scalar, Eigen::Dynamic, 1>`
`dyn_row_vect<Scalar>` |  Dynamic row vector template alias over the field `Scalar`, alias for `Eigen::Matrix<Scalar, 1, Eigen::Dynamic>`
Table 1: User-defined data types

###  3.2 Constants

The important constants are defined in the header file `constants.h` and are
listed in Table 2.

`constexpr idx maxn = 64;` | Maximum number of allowed qu(d)its (subsystems)
---|---
`constexpr double pi = 3.1415...;` | $\pi$
`constexpr double ee = 2.7182...;` |  $e$, base of natural logarithms
`constexpr double eps = 1e-12;` | Used in comparing floating point values to zero
`constexpr double chop = 1e-10;` | Used in display manipulators to set numbers to zero
`constexpr double infty = ...;` | Used to denote infinity in double precision
`constexpr cplx operator""_i` ` (unsigned long long int x)` | User-defined literal for the imaginary number $i:=\sqrt{-1}$
`constexpr cplx operator""_i` ` (unsigned long double int x)` | User-defined literal for the imaginary number $i:=\sqrt{-1}$
`cplx omega(idx D)` |  $D$-th root of unity $e^{2\pi i/D}$
Table 2: User-defined constants

###  3.3 Singleton classes and their global instances

Some useful classes are defined as singletons and their instances are globally
available, being initialized at runtime in the header file `qpp.h`, before
`main()`. They are listed in Table 3.

`const Init& init = Init::get_instance();` | Library initialization
---|---
`const Codes& codes = Codes::get_instance();` | Quantum error correcting codes
`const Gates& gt = Gates::get_instance();` | Quantum gates
`const States& st = States::get_instance();` | Quantum states
`RandomDevices& rdevs =` ` RandomDevices::get_thread_local_instance();` | Random devices/generators/engines
Table 3: Global singleton classes and instances

##  4 Simple examples

All of the examples of this section are copied verbatim from the directory
`./examples` and are fully compilable. For convenience, the location of the
source file is displayed in the first line of each example as a C++ comment.
The examples are simple and demonstrate the main features of Quantum++. They
cover only a small part of library functions, but enough to get the interested
user started. For an extensive reference of all library functions, including
various overloads, the user should consult the complete reference
`./doc/refman.pdf`. See the rest of the examples (not discussed in this
document) in `./examples/`. for more comprehensive code snippets.

###  4.1 Gates and states

We introduce the main objects used by Quantum++: gates, states and basic
operations. Consider the code in Listing 2.

⬇

1// Gates and states

2// Source: ./examples/gates_states.cpp

3#include <iostream>

4#include "qpp.h"

5

6using namespace qpp;

7

8int main()

9{

10 ket psi = st.z0; // |0> state

11 cmat U = gt.X;

12 ket result = U * psi;

13

14 std::cout << ">> The result of applying the bit-flip gate X on |0> is:\n";

15 std::cout << disp(result) << ’\n’;

16

17 psi = mket({1, 0}); // |10> state

18 U = gt.CNOT; // Controlled-NOT

19 result = U * psi;

20

21 std::cout << ">> The result of applying the gate CNOT on |10> is:\n";

22 std::cout << disp(result) << ’\n’;

23

24 U = randU(2);

25 std::cout << ">> Generating a random one-qubit gate U:\n";

26 std::cout << disp(U) << ’\n’;

27

28 result = applyCTRL(psi, U, {0}, {1}); // Controlled-U

29 std::cout << ">> The result of applying the CTRL-U gate on |10> is:\n";

30 std::cout << disp(result) << ’\n’;

31}

Listing 2: Gates and states

A possible output is:

⬇

>> The result of applying the bit-flip gate X on |0> is:

0

1.0000

>> The result of applying the gate CNOT on |10> is:

0

0

0

1.0000

>> Generating a random one-qubit gate U:

0.9147 - 0.3840i -0.1245 - 0.0196i

0.1056 + 0.0688i 0.4223 + 0.8977i

>> The result of applying the CTRL-U gate on |10> is:

0

0

0.9147 - 0.3840i

0.1056 + 0.0688i

Listing 2 output

In line 4 of Listing 2 we bring the namespace `qpp` into the global namespace.

In line 10 we use the `States` singleton `st` to declare `psi` as the zero
eigenvector $|0\rangle$ of the $Z$ Pauli operator. In line 11 we use the
`Gates` singleton `gt` and assign to `U` the bit flip gate `gt.X`. In line 12
we compute the result of the operation $X|0\rangle$, and display the result
$|1\rangle$ in lines 14 and 15. In line 15 we use the display manipulator
`disp()`, which is especially useful when displaying complex matrices, as it
displays the entries of the latter in the form $a+bi$, in contrast to the form
$(a,b)$ used by the C++ standard library. The manipulator also accepts
additional parameters that allows e.g. setting to zero numbers smaller than
some given value (useful to chop small values), and it is in addition
overloaded for standard containers, iterators and C-style arrays.

In line 17 we reassign to `psi` the state $|10\rangle$ via the function
`mket()`. We could have also used the Eigen 3 insertion operator

⬇

ket psi(4); // must specify the dimension before insertion of elements via <<

psi << 0, 0, 1, 0;

however the `mket()` function is more concise. In line 18 we declare a gate
`U` as the Controlled-NOT with control as the first subsystem, and target as
the last, using the global singleton `gt`. In line 19 we declare the ket
`result` as the result of applying the Controlled-NOT gate to the state
$|10\rangle$, i.e. $|11\rangle$. We then display the result of the computation
in lines 21 and 22.

Next, in line 24 we generate a random unitary gate via the function `randU()`,
then in line 28 apply the Controlled-U, with control as the first qubit and
target as the second qubit, to the state `psi`. Finally, we display the result
in lines 29 and 30.

###  4.2 Measurements

Let us now complicate things a bit and introduce measurements. Consider the
example in Listing 3.

⬇

1// Measurements

2// Source: ./examples/measurements.cpp

3#include <iostream>

4#include <tuple>

5#include "qpp.h"

6

7using namespace qpp;

8

9int main()

10{

11 ket psi = mket({0, 0});

12 cmat U = gt.CNOT * kron(gt.H, gt.Id2);

13 ket result = U * psi; // we have the Bell state (|00> + |11>) / sqrt(2)

14

15 std::cout << ">> We just produced the Bell state:\n";

16 std::cout << disp(result) << ’\n’;

17

18 // apply a bit flip on the second qubit

19 result = apply(result, gt.X, {1}); // we produced (|01> + |10>) / sqrt(2)

20 std::cout << ">> We produced the Bell state:\n";

21 std::cout << disp(result) << ’\n’;

22

23 // measure the first qubit in the X basis

24 auto measured = measure(result, gt.H, {0});

25 std::cout << ">> Measurement result: " << std::get<0>(measured) << ’\n’;

26 std::cout << ">> Probabilities: ";

27 std::cout << disp(std::get<1>(measured), ", ") << ’\n’;

28 std::cout << ">> Resulting states:\n";

29 for (auto&& it : std::get<2>(measured))

30 std::cout << disp(it) << ’\n’;

31}

Listing 3: Measurements

A possible output is:

⬇

>> We just produced the Bell state:

0.7071

0

0

0.7071

>> We produced the Bell state:

0

0.7071

0.7071

0

>> Measurement result: 1

>> Probabilities: [0.5000, 0.5000]

>> Resulting states:

0.7071

0.7071

-0.7071

0.7071

Listing 3 output

In line 12 of Listing 3 we use the function `kron()` to create the tensor
product (Kronecker product) of the Hadamard gate on the first qubit and
identity on the second qubit, then we left-multiply it by the Controlled-NOT
gate. In line 13 we compute the result of the operation $CNOT_{ab}(H\otimes
I)|00\rangle$, which is the Bell state $(|00\rangle+|11\rangle)/\sqrt{2}$. We
display it in lines 15 and 16.

In line 19 we use the function `apply()` to apply the gate $X$ on the second
qubit222Quantum++ uses the C/C++ numbering convention, with indexes starting
from zero. of the previously produced Bell state. The function `apply()` takes
as its third parameter a list of subsystems, and in our case `{1}` denotes the
_second_ subsystem, not the first. The function `apply()`, as well as many
other functions that we will encounter, have a variety of useful overloads,
see `doc/refman.pdf` for a detailed library reference. In lines 20 and 21 we
display the newly created Bell state.

In line 24 we use the function `measure()` to perform a measurement of the
first qubit (subsystem `{0}`) in the $X$ basis. You may be confused by the
apparition of `gt.H`, however this overload of the function `measure()` takes
as its second parameter the measurement basis, specified as the columns of a
complex matrix. In our case, the eigenvectors of the $X$ operator are just the
columns of the Hadamard matrix. As mentioned before, as all other library
functions, `measure()` returns by value, hence it does not modify its
argument. The return of `measure` is a tuple consisting of the measurement
result, the outcome probabilities, and the possible output states. Technically
`measure()` returns a tuple of 3 elements

⬇

std::tuple<qpp::idx, std::vector<double>, std::vector<qpp::cmat>>

The first element represents the measurement result, the second the possible
output probabilities and the third the output output states. Instead of using
this long type definition, we use the new C++11 `auto` keyword to define the
type of the result `measured` of `measure()`. In lines 25–30 we use the
standard `std::get<>()` function to retrieve each element of the tuple, then
display the measurement result, the probabilities and the resulting output
states.

###  4.3 Quantum operations

In Listing 4 we introduce quantum operations: quantum channels, as well as the
partial trace and partial transpose operations.

⬇

1// Quantum operations

2// Source: ./examples/quantum_operations.cpp

3#include <iostream>

4#include <vector>

5#include "qpp.h"

6

7using namespace qpp;

8

9int main()

10{

11 cmat rho = st.pb00; // projector onto the Bell state (|00> + |11>) /
sqrt(2)

12 std::cout << ">> Initial state:\n";

13 std::cout << disp(rho) << ’\n’;

14

15 // partial transpose of first subsystem

16 cmat rhoTA = ptranspose(rho, {0});

17 std::cout << ">> Eigenvalues of the partial transpose "

18 << "of Bell-0 state are:\n";

19 std::cout << disp(transpose(hevals(rhoTA))) << ’\n’;

20

21 std::cout << ">> Measurement channel with 2 Kraus operators:\n";

22 std::vector<cmat> Ks{st.pz0, st.pz1}; // 2 Kraus operators

23 std::cout << disp(Ks[0]) << "\n and \n" << disp(Ks[1]) << ’\n’;

24

25 std::cout << ">> Superoperator matrix of the channel:\n";

26 std::cout << disp(kraus2super(Ks)) << ’\n’;

27

28 std::cout << ">> Choi matrix of the channel:\n";

29 std::cout << disp(kraus2choi(Ks)) << ’\n’;

30

31 // apply the channel onto the first subsystem

32 cmat rhoOut = apply(rho, Ks, {0});

33 std::cout << ">> After applying the measurement channel "

34 << "on the first qubit:\n";

35 std::cout << disp(rhoOut) << ’\n’;

36

37 // take the partial trace over the second subsystem

38 cmat rhoA = ptrace(rhoOut, {1});

39 std::cout << ">> After partially tracing down the second subsystem:\n";

40 std::cout << disp(rhoA) << ’\n’;

41

42 // compute the von-Neumann entropy

43 double ent = entropy(rhoA);

44 std::cout << ">> Entropy: " << ent << ’\n’;

45}

Listing 4: Quantum operations

The output of this program is:

⬇

>> Initial state:

0.5000 0 0 0.5000

0 0 0 0

0 0 0 0

0.5000 0 0 0.5000

>> Eigenvalues of the partial transpose of Bell-0 state are:

-0.5000 0.5000 0.5000 0.5000

>> Measurement channel with 2 Kraus operators:

1.0000 0

0 0

and

0 0

0 1.0000

>> Superoperator matrix of the channel:

1.0000 0 0 0

0 0 0 0

0 0 0 0

0 0 0 1.0000

>> Choi matrix of the channel:

1.0000 0 0 0

0 0 0 0

0 0 0 0

0 0 0 1.0000

>> After applying the measurement channel on the first qubit:

0.5000 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0.5000

>> After partially tracing down the second subsystem:

0.5000 0

0 0.5000

>> Entropy: 1.0000

Listing 4 output

The example should by now be self-explanatory. In line 11 of Listing 4 we
define the input state `rho` as the projector onto the Bell state
$(|00\rangle+|11\rangle)/\sqrt{2}$, then display it in lines 12 and 13.

In lines 16–19 we partially transpose the first qubit, then display the
eigenvalues of the resulting matrix `rhoTA`.

In lines 21–23 we define a quantum channel `Ks` consisting of two Kraus
operators: $|0\rangle\langle 0|$ and $|1\rangle\langle 1|$, then display the
latter. Note that Quantum++ uses the `std::vector<cmat>` container to store
the Kraus operators and define a quantum channel.

In lines 25–29 we display the superoperator matrix as well as the Choi matrix
of the channel `Ks`.

Next, in lines 32–35 we apply the channel `Ks` to the first qubit of the input
state `rho`, then display the output state `rhoOut`.

In lines 38–40 we take the partial trace of the output state `rhoOut`, then
display the resulting state `rhoA`.

Finally, in lines 43 and 44 we compute the von-Neumann entropy of the
resulting state and display it.

###  4.4 Timing

To facilitate simple timing tasks, Quantum++ provides a `Timer` class that
uses internally a `std::steady_clock`. The program in Listing 5 demonstrate
its usage.

⬇

1// Timing

2// Source: ./examples/timing.cpp

3#include <iomanip>

4#include <iostream>

5#include <vector>

6#include "qpp.h"

7

8using namespace qpp;

9

10int main()

11{

12 std::cout << std::setprecision(8); // increase the default output precision

13

14 // get the first codeword from Shor’s [[9,1,3]] code

15 ket c0 = codes.codeword(Codes::Type::NINE_QUBIT_SHOR, 0);

16

17 Timer<> t; // declare and start a timer

18 std::vector<idx> perm = randperm(9); // declare a random permutation

19 ket c0perm = syspermute(c0, perm); // permute the system

20 t.toc(); // stops the timer

21 std::cout << ">> Permuting subsystems according to " << disp(perm, ", ");

22 std::cout << "\n>> It took " << t << " seconds to permute the
subsytems.\n";

23

24 t.tic(); // restart the timer

25 std::cout << ">> Inverse permutation: ";

26 std::cout << disp(invperm(perm), ", ") << ’\n’;

27 ket c0invperm = syspermute(c0perm, invperm(perm)); // permute again

28 std::cout << ">> It took " << t.toc();

29 std::cout << " seconds to un-permute the subsystems.\n";

30

31 std::cout << ">> Norm difference: " << norm(c0invperm - c0) << ’\n’;

32}

Listing 5: Timing

A possible output of this program is:

⬇

>> Permuting subsystems according to [7, 2, 3, 1, 8, 0, 4, 5, 6]

>> It took 0.00021400 seconds to permute the subsytems.

>> Inverse permutation: [5, 3, 1, 2, 6, 7, 8, 0, 4]

>> It took 0.00008100 seconds to un-permute the subsystems.

>> Norm difference: 0.00000000

Listing 5 output

In line 12 of Listing 5 we change the default output precision from 4 to 8
decimals after the delimiter.

In line 15 we use the `Codes` singleton `codes` to retrieve in `c0` the first
codeword of the Shor’s $[[9,1,3]]$ quantum error correcting code.

In line 17 we declare an instance `timer` of the class `Timer`. In line 18 we
declare a random permutation `perm` via the function `randperm()`. In line 19
we permute the codeword according to the permutation `perm` using the function
`syspermute()` and store the result . In line 20 we stop the timer. In line 21
we display the permutation, using an overloaded form of the `disp()`
manipulator for C++ standard library containers. The latter takes a
`std::string` as its second parameter to specify the delimiter between the
elements of the container. In line 22 we display the elapsed time using the
`ostream operator<<()` operator overload for `Timer` objects.

Next, in line 24 we reset the timer, then display the inverse permutation of
`perm` in lines 25 and 26. In line 27 we permute the already permuted state
`c0perm` according to the inverse permutation of `perm`, and store the result
in `c0invperm`. In lines 28 and 29 we display the elapsed time. Note that in
line 28 we used directly `t.toc()` in the stream insertion operator, since,
for convenience, the member function `Timer::toc()` returns a `const Timer&`.

Finally, in line 31, we verify that by permuting and permuting again using the
inverse permutation we recover the initial codeword, i.e. the norm difference
has to be zero.

###  4.5 Input/output

We now introduce the input/output functions of Quantum++, as well as the
input/output interfacing with MATLAB. The program in Listing 6 saves a matrix
in both Quantum++ internal format as well as in MATLAB format, then loads it
back and tests that the norm difference between the saved/loaded matrix is
zero.

⬇

1// Input/output

2// Source: ./examples/input_output.cpp

3#include <iostream>

4#include "qpp.h"

5#include "MATLAB/matlab.h" // must be explicitly included

6

7using namespace qpp;

8

9int main()

10{

11 // Quantum++ native input/output

12 cmat rho = randrho(256); // an 8 qubit density operator

13 save(rho, "rho.dat"); // save it

14 cmat loaded_rho = load<cmat>("rho.dat"); // load it back

15 // display the difference in norm, should be 0

16 std::cout << ">> Norm difference load/save: ";

17 std::cout << norm(loaded_rho - rho) << ’\n’;

18

19 // interfacing with MATLAB

20 saveMATLABmatrix(rho, "rho.mat", "rho", "w");

21 loaded_rho = loadMATLABmatrix<cmat>("rho.mat", "rho");

22 // display the difference in norm, should be 0

23 std::cout << ">> Norm difference MATLAB load/save: ";

24 std::cout << norm(loaded_rho - rho) << ’\n’;

25}

Listing 6: Input/output

The output of this program is:

⬇

>> Norm difference load/save: 0.0000

>> Norm difference MATLAB load/save: 0.0000

Listing 6 output

Note that in order to use the MATLAB input/output interface support, you need
to explicitly include the header file `MATLAB/matlab.h`, and you also need to
have MATLAB or MATLAB compiler installed, otherwise the program fails to
compile. See the file `./README.md` for extensive details about compiling with
MATLAB support.

###  4.6 Exceptions

Most Quantum++ functions throw exceptions in the case of unrecoverable errors,
such as out-of-range input parameters, input/output errors etc. The exceptions
are handled via the class `Exception`, derived from `std::exception`. The
exception types are hard-coded inside the strongly-typed enumeration (enum
class) `Exception::Type`. If you want to add more exceptions, augment the
enumeration `Exception::Type` and also modify accordingly the member function
`Exception::construct_exception_msg_()`, which constructs the exception
message displayed via the overridden virtual function `Exception::what()`.
Listing 7 illustrates the basics of exception handling in Quantum++.

⬇

1// Exceptions

2// Source: ./examples/exceptions.cpp

3#include <exception>

4#include <iostream>

5#include "qpp.h"

6

7using namespace qpp;

8

9int main()

10{

11 cmat rho = randrho(16); // 4 qubits (subsystems)

12 try

13 {

14 double mInfo = qmutualinfo(rho, {0}, {4}); // throws qpp::Exception

15 std::cout << ">> Mutual information between first and last subsystem: ";

16 std::cout << mInfo << ’\n’;

17 }

18 catch (const std::exception& e)

19 {

20 std::cout << ">> Exception caught: " << e.what() << ’\n’;

21 }

22}

Listing 7: Exceptions

The output of this program is:

⬇

>> Exception caught: IN qpp::qmutualinfo(): Subsystems mismatch dimensions!

Listing 7 output

In line 11 of Listing 7 we define a random density matrix on four qubits
(dimension 16). In line 14, we compute the mutual information between the
first and the 5-th subsystem. Line 14 throws an `Exception` of type
`Exception::Type::SUBSYS_MISMATCH_DIMS`, as there are only four systems. We
next catch the exception in line 18 via the `std::exception` base class. We
could have also used directly the class `Exception`, however using the base
class allows the catching of other exceptions, not just of the type
`Exception`. Finally, in line 20 we display the corresponding exception
message.

##  5 Brief description of Quantum++ file structure

A brief description of the Quantum++ file structure is presented in Figure 1.
The directories and their brief descriptions are emphasized using bold fonts.
The main header file qpp.h is emphasized in red fonts.

.1  ./. .2 doc/ Documentation. .3 html/ HTML documentation. .4 index.html Main
HTML documentation file. .3 quick.pdf Quick reference guide. .3 refman.pdf
Complete reference. .2 examples/ Usage examples. .3 ex*.cpp Example source
files. .2 include/ Header files. .3 MATLAB/ MATLAB support. .4 matlab.h
Input/output interfacing with MATLAB. .3 classes/ Class definitions. .4
codes.h Quantum error correcting codes. .4 exception.h Exceptions. .4 gates.h
Quantum gates. .4 idisplay.h Interface for stream extraction. .4 init.h
Initialization. .4 random_devices.h Random devices. .4 states.h Quantum
states. .4 timer.h Timing. .3 experimental/ Experimental/test
functions/classes, do not use or modify. .4 experimental.h Experimental/test
functions/classes. .3 internal/ Internal implementation details, do not
use/modify. .4 classes/ Internal class definitions. .5 iomanip.h Input/output
manipulators. .5 singleton.h Singleton pattern via CRTP. .4 util.h Internal
utility functions. .3 constants.h Constants. .3 entanglement.h Entanglement
functions. .3 entropies.h Entropy functions. .3 functions.h Generic quantum
computing functions. .3 input_output.h Input/output functions. .3
instruments.h Measurement functions. .3 macros.h Preprocessor macros. .3
number_theory.h Number theory functions. .3 operations.h Quantum operation
functions. .3 qpp.h Quantum++ main header file, includes all other necessary
headers. .3 random.h Randomness functions. .3 statistics.h Statistics
functions. .3 traits.h Type traits. .3 types.h Type aliases. .2 unit_tests/
Unit testing with Google Mock/Test (included in lib). .2 CHANGES Modifications
added by each version. .2 CMakeLists.txt cmake configuration file, builds
./examples/minimal.cpp and the unit tests. .2 COPYING GNU General Public
License version 3. .2 README.md Building instructions. .2 RELEASE.md Release
notes. .2 VERSION Version number. .2 run_OSX_MATLAB Script for running with
MATLAB support under OS X.

Figure 1: Quantum++ file structure

##  6 Advanced topics

###  6.1 Aliasing

Aliasing occurs whenever the same Eigen 3 matrix/vector appears on both sides
of the assignment operator, and happens because of Eigen 3’s lazy evaluation
system. Examples that exhibit aliasing:

⬇

mat = 2 * mat;

or

⬇

mat = mat.transpose();

Aliasing _does not_ occur in statements like

⬇

mat = f(mat);

where `f()` returns by value. Aliasing produces in general unexpected results,
and should be avoided at all costs.

Whereas the first line produces aliasing, it is not dangerous, since the
assignment is done in a one-to-one manner, i.e. each element $(i,j)$ on the
left hand side of the assignment operator is solely a function of the the
_same_ $(i,j)$ element on the right hand side, i.e. $mat(i,j)=f(mat(i,j))$,
$\forall i,j$. The problem appears whenever coefficients are being combined
and overlap, such as in the second example, where $mat(i,j)=mat(j,i)$,
$\forall i,j$. To avoid aliasing, use the member function `eval()` to
transform the right hand side object into a temporary, such as

⬇

mat = 2 * mat.eval();

In general, aliasing can not be detected at compile time, but can be detected
at runtime whenever the compile flag `EIGEN_NO_DEBUG` is not set. Quantum++
does not set this flag in debug mode. I highly recommend to first compile your
program in debug mode to detect aliasing run-time assertions, as well as other
possible issues that may have escaped you, such as assigning to a matrix
another matrix of different dimension etc.

For more details about aliasing, see the official Eigen 3 documentation at
http://eigen.tuxfamily.org/dox/group__TopicAliasing.html.

###  6.2 Type deduction via auto

Avoid the usage of `auto` when working with Eigen 3 expressions, e.g. avoid
writing code like

⬇

auto mat = A * B + C;

but write instead

⬇

cmat mat = A * B + C;

as otherwise there is a slight possibility of getting unexpected results. The
“problem” lies in Eigen 3 lazy evaluation system and reference binding, see
e.g. http://stackoverflow.com/q/26705446/3093378 for more details.

###  6.3 Optimizations

Whenever testing your application, I recommend compiling in debug mode, as
Eigen 3 run-time assertions can provide extremely helpful feedback on
potential issues. Whenever the code is production-ready, you should _always_
compile with optimization flags turned on, such as `-O3` (for g++) and
`-DEIGEN_NO_DEBUG`. You should also turn on the OpenMP (if available) multi-
processing flag (`-fopenmp` for g++), as it enables multi-core/multi-
processing with shared memory. Eigen 3 uses multi-processing when available,
e.g. in matrix multiplication. Quantum++ also uses multi-processing in
computationally-intensive functions.

Since most Quantum++ functions return by value, in assignments of the form

⬇

mat = f(another_mat);

there is an additional copy assignment operator when assigning the temporary
returned by `f()` back to `mat`. As far as I know, this extra copy operation
is not elided. Unfortunately, Eigen 3 does not yet support move semantics,
which would have got rid of this additional assignment via the corresponding
move assignment operator. If in the future Eigen 3 will support move
semantics, the additional assignment operator will be “free”, and you won’t
have to modify any existing code to enable the optimization; the Eigen 3 move
assignment operator should take care of it for you.

Note that in a line of the form

⬇

cmat mat = f(another_mat);

most compilers perform return value optimization (RVO), i.e. the temporary on
the right hand side is constructed directly inside the object `mat`, the copy
constructor being elided.

###  6.4 Extending Quantum++

Most Quantum++ operate on Eigen 3 matrices/vectors, and return either a matrix
or a scalar. In principle, you may be tempted to write a new function such as

⬇

cmat f(const cmat& A){...}

The problem with the approach above is that Eigen 3 uses _expression
templates_ as the type of each expression, i.e. different expressions have in
general different types, see the official Eigen 3 documentation at
http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html for more
details. The correct way to write a generic function that is guaranteed to
work with any matrix expression is to make your function template and declare
the input parameter as `Eigen::MatrixBase<Derived>`, where `Derived` is the
template parameter. For example, the Quantum++ `transpose()` function is
defined as

⬇

1template<typename Derived>

2dyn_mat<typename Derived::Scalar>

3transpose(const Eigen::MatrixBase<Derived>& A)

4{

5 const dyn_mat<typename Derived::Scalar>& rA = A.derived();

6

7 // check zero-size

8 if (!internal::check_nonzero_size(rA))

9 throw Exception("qpp::transpose()", Exception::Type::ZERO_SIZE);

10

11 return rA.transpose();

12}

It takes an Eigen 3 matrix expression, line 3, and returns a dynamic matrix
over the scalar field of the expression, line 2. In line 5 we implicitly
convert the input expression `A` to a dynamic matrix `rA` over the same scalar
field as the expression, via binding to a `const` reference, therefore paying
no copying cost. We then use `rA` instead of the original expression `A` in
the rest of the function. Note that most of the time it is OK to use the
original expression, however there are some cases where you may get a compile
time error if the expression is not explicitly casted to a matrix. For
consistency, I use this reference binding trick in the code of all Quantum++
functions.

As you may have already seen, Quantum++ consists mainly of a collection of
functions and few classes. There is no complicated class hierarchy, and you
can regard the Quantum++ API as a medium-level API. You may extend it to
incorporate graphical input, e.g. use a graphical library such as Qt, or build
a more sophisticated library on top of it. I recommend to read the source code
and make yourself familiar with the library before deciding to extend it. You
should also check the complete reference manual `./doc/refman.pdf` for an
extensive documentation of all functions and classes. I hope you find
Quantum++ useful and wish you a happy usage!

## Acknowledgements

I acknowledge financial support from Industry Canada and from the Natural
Sciences and Engineering Research Council of Canada (NSERC). I thank Kassem
Kalach for carefully reading this manuscript and providing useful suggestions.

## References

  * [1] List of QC simulators, available online at  http://www.quantiki.org/wiki/List_of_QC_simulators.  Last accessed: .
  * [2] Michael A. Nielsen and Isaac L. Chuang.  Quantum Computation and Quantum Information.  Cambridge University Press, Cambridge, 5th edition, 2000.